I am working on a Legacy app and this is driving me crazy, every example I have seen addressing this issue appears to do what I am doing but for some reason my reportProgress event refuses to fire:
Private _worker as BackgroundWorker
Private Sub btnEditCheckSymbol_Click (ByVal sender As Object, ByVal e As EventArgs) Handles btnEditCheckSymbol.Click
_worker = new BackgroundWorker()
_worker.WorkerSupportsCancellation = True
_worker.WorkerReportsProgress = True
AddHandler _worker.DoWork, AddressOf worker_DoWork
AddHandler _worker.ProgressChanged, AddressOf worker_ProgressChanged
AddHandler _worker.RunWorkerCompleted, AddressOf worker_RunWorkerCompleted
_worker.RunWorkerAsync()
'Other long running code
If _worker.IsBusy Then
_worker.CancelAsync()
End If
End Sub
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
For i As Integer = 60 To 0 step -1
If _worker.CancellationPending Then
e.Cancel = True
return
End If
_worker.ReportProgress(i)
Next
End Sub
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
lbEditCheckSymbol.Invoke(
Sub ()
with lbEditCheckSymbol
.Items.Clear()
.Items.Add("Please wait for results: " + e.ProgressPercentage)
end With
End Sub)
Thread.Sleep(100)
End Sub
Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
with lbEditCheckSymbol
.Items.Clear()
If Not e.Cancelled then
lbEditCheckSymbol.Invoke(
Sub ()
with lbEditCheckSymbol
.Items.Add("Results should have arrived by now... um wait some more?")
end With
End Sub)
End If
end With
End Sub
The DoWork is called and it burns through the loop never invoking the ProgressChanged event, I checked in the debugger and the ProgressChanged is registered. I mostly work in C# so am I missing some VB voodoo here?
UPDATE
I have abandoned the use of the background worker in favor of the TPL library. Less code, easier to debug and I actually got it to work so win, win, win.
If you had better error handlers around your threaded work, you would catch the access violation when your background thread tries to access objects on the foreground thread. To reach-across threads like that, you need some delegates and use BeginInvoke instead of Invoke to call over to the right thread.
This code might not be perfect, but it should be pretty close
Private _worker as BackgroundWorker
Public Delegate Sub ProgressChangedDelegateType(percent As Integer)
Public Delegate Sub RunWorkerCompletedDelegateType(cancelled As Boolean)
Private progressChangedDelegate As ProgressChangedDelegateType
Private runWorkerCompletedDelegate AS RunWorkerCompletedDelegateType
Public Sub Form_Load()
'bind your delegates in your form load event or in the class constructor
progressChangedDelegate = AddressOf ProgressChanged
runWorkerCompletedDelegate = AddressOf RunWorkerCompleted
'and all of your other stuff in your form_load() event handler...
End Sub
Private Sub btnEditCheckSymbol_Click (ByVal sender As Object, ByVal e As EventArgs) Handles btnEditCheckSymbol.Click
_worker = new BackgroundWorker()
_worker.WorkerSupportsCancellation = True
_worker.WorkerReportsProgress = True
AddHandler _worker.DoWork, AddressOf worker_DoWork
AddHandler _worker.ProgressChanged, AddressOf worker_ProgressChanged
AddHandler _worker.RunWorkerCompleted, AddressOf worker_RunWorkerCompleted
_worker.RunWorkerAsync()
'Other long running code
If _worker.IsBusy Then
_worker.CancelAsync()
End If
End Sub
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) _
For i As Integer = 60 To 0 step -1
If _worker.CancellationPending Then
e.Cancel = True
return
End If
_worker.ReportProgress(i)
Next
End Sub
Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
If lbEditCheckSymbol.InvokeRequired Then
BeginInvoke(progressChangedDelgate, {e.ProgressPercentage})
Else
ProgressChanged(e.ProgressPercentage)
End If
End Sub
Public Sub ProgressChanged(percent As Integer)
Try
with lbEditCheckSymbol
.Items.Clear()
.Items.Add("Please wait for results: " + percent.ToString())
end With
DoEvents() 'forces a UI update
Catch Ex As Exception
Debug.WriteLine(ex.message) 'put a breakpoint here, just in case
End Try
Thread.Sleep(100)
End Sub
Private Sub worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
If lbEditCheckSymbol.InvokeRequired Then
BeginInvoke(runWorkerCompletedDelegate, {e.Cancelled})
Else
RunWorkerCompleted(e.Cancelled)
End If
End Sub
Public Sub RunWorkerCompleted(cancelled As Boolean)
Try
with lbEditCheckSymbol
.Items.Clear()
If Not cancelled then
.Items.Add("Results should have arrived by now... um wait some more?")
end With
DoEvents() 'Forces a UI update
Catch Ex As Exception
Debug.WriteLine(ex.message) 'put a breakpoint here, just in case
End Try
End Sub
Related
I am using the following code to get the size of files inside a directory
and put it in Label1:
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
"\windows",Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly,_
"*.*")
Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile)
Label1.Text = Label1.Text + filesizelabel.Length
Next
The problem is that i have more than 50 for each loops (a system cleaning app).
When I run the loops my app freezes until the loops finish, even if I run one loop.
Is there a solution to make it show the name of the current file? I tried this as well, but it also froze my application:
label2.text = foundfile
The application does not respond to any click, until it finishes the loops. It shows the size in Label1 and the last scanned file in Label2. This also freezes the application:
system.threading.thread.sleep(100)
Is there any alternative to foreach or a solution to fix this issue?
Here's a quick example using Async/Await with a Button Click() Handler:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Await Task.Run(Sub()
' this runs in a different thread without blocking the GUI:
For Each foundFile As String In My.Computer.FileSystem.GetFiles(
"\windows", Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.*")
Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile)
' when you need to update the GUI:
Me.Invoke(Sub()
' ... do it in here ...
Label1.Text = Label1.Text + filesizelabel.Length
End Sub)
Next
End Sub)
Button1.Enabled = True
End Sub
For VB.Net 2010, try this instead:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Dim T As New System.Threading.Thread(AddressOf Worker)
T.Start()
End Sub
Private Sub Worker()
' this runs in a different thread without blocking the GUI:
For Each foundFile As String In My.Computer.FileSystem.GetFiles(
"\windows", Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.*")
Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile)
' when you need to update the GUI:
Me.Invoke(Sub()
' ... do it in here ...
Label1.Text = Label1.Text + filesizelabel.Length
End Sub)
Next
Me.Invoke(Sub()
Button1.Enabled = True
End Sub)
End Sub
This is a prime candidate for a background worker.
Have a read about how they work, but at a high level the task is run in another thread with some events that you access in your main UI thread.
Private bw As BackgroundWorker = New BackgroundWorker
Private Sub buttonStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
If Not bw.IsBusy = True Then
' this will start the work
bw.RunWorkerAsync()
End If
End Sub
Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
If bw.WorkerSupportsCancellation = True Then
' this will allow the user to cancel the work part way through
bw.CancelAsync()
End If
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
' your slow code goes here
End Sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
' you can update the UI here to show progress
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
' your 'I've finished notification' code goes here
End Sub
Derek has put a bad code, it's not working and Idle mind code does not work on .NET 2.0
Dereks approach is working if code is complete, as below:
Private bw As BackgroundWorker = New BackgroundWorker
Private Sub app_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
..
End sub
Private Sub Btn_Click(sender As Object, e As EventArgs) Handles Btn.Click
If Not bw.IsBusy = True Then
' this will start the work
bw.RunWorkerAsync()
End If
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'your work to not freeze form
end sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
' you can update the UI here to show progress
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
' your 'I've finished notification' code goes here
End Sub
I have a main form with a progressbar at the bottom status strip. It is set to marquee style. I want it to keep animating when
I run a long function. But when the function hits, the form and everything on it freezes, so II used a backgroundworker
to run the long function. But this gave me the following error inside the MyClass.BigFunction() code.
Cross-thread operation not valid: Control 'frmMainNew' accessed from a thread other than the thread it was created on.
"frmMainNew" is the main form on which the progressbar and backgroundworker are. I pass the form as a parameter to the MyClass object
when I initialize it.
This is the first time I am using backgroundworker, so what else do I need?
I have already looked at these examples and tried them, but nothing works. (1, 2, 3, 4, 5, 6).
I have to use this for other functions too.
My code:
Private WithEvents bgw As BackgroundWorker
Private Sub frmMainNew_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Me.SuspendLayout()
'Other Functions
w_AddBackgroundWorkerForProgressBar()
Me.ResumeLayout()
Catch ex As Exception
Scube.Planner.ErrorHandler.DisplayError(ex)
End Try
End Sub
Private Sub w_AddBackgroundWorkerForProgressBar()
bgw = New BackgroundWorker
AddHandler bgw.DoWork, AddressOf bgw_DoWork
AddHandler bgw.RunWorkerCompleted, AddressOf bgw_Completed
'AddHandler bgw.ProgressChanged, AddressOf bgw_ProgressChanged
End Sub
Private Sub MyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
Try
bgw.WorkerSupportsCancellation = True
bgw.RunWorkerAsync()
'MyClass.BigFunction() <--- Originally called from here
Catch ex As Exception
Scube.Planner.ErrorHandler.DisplayError(ex)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bgw.DoWork
'Do your lenghty operations here
MyClass.BigFunction()
System.Threading.Thread.Sleep(10000)
End Sub
Private Sub bgw_Completed(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
If Not IsNothing(e.Error) Then
MessageBoxEx.Show(e.Error.ToString)
End If
ProgressBar1.Hide()
End Sub
Private Sub w_ShowProgressBar()
ProgressBar1.Show()
Me.Refresh()
System.Windows.Forms.Application.DoEvents()
End Sub
You cant and should not access your frmMainNew from your 'BigFunction()'. It is working on a separate thread and does not have access to the UI thread. We need to see what you are doing inside your bigfunction to tell you the problem. Im going to take a guess and say you are trying to update the progressbar values from within that function? If this is so then is the incorrect way to do so.
What you need to do is, set the progress in your BigFunction like so:
bgw.ReportProgress(Progress/Percentage)
and have an event for the progress changed and inside of that event is where you update the progress bar.
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Progressbar1.value = e.ProgressPercentage
End Sub
Im writing an app that takes and copy's folders (selected by the user in a listbox) to a specified location, it has a progress bar that should run the whole time but it seems that the backgroundworker isnt catching the process.. it just freezes the UI until the copy is complete.. when I put in a message between copy's the message works so I know it is capable of working.. im missing something..
Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Btn_SaveApps.Click
bw.WorkerSupportsCancellation = True
bw.WorkerReportsProgress = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
If Not bw.IsBusy = True Then
bw.RunWorkerAsync(Module1.SaveApp)
End If
End Sub
Module Module1
Function SaveApp() As Process
Dim WinStrApps As String = ("C:\TransferFrom")
Form1.tbProgress.Style = ProgressBarStyle.Marquee
For Each Item In Form1.Selected_Apps.SelectedItems
On Error Resume Next
Form1.tbProgress.Visible = True
Dim FileLoc = ("C:\TransferTo\")
If System.IO.Directory.Exists(WinStrApps + Item) = True Then
Directory.CreateDirectory(FileLoc + Item)
My.Computer.FileSystem.CopyDirectory(WinStrApps + Item, FileLoc + Item)
Else
MsgBox(Item + " does not exist, or is a system App. Please choose another application")
End If
Next
MsgBox("Completed saving the applications")
End Function
I think you may have called the thread wrong, the thread has to begin in the sub bw_DoWork
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Module1.SaveApp
end sub
and any updates passed back to the UI thread need to be passed to
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
Form1.tbProgress.value = e.ProgressPercentage
End Sub
however you wont be able to access the update routine from your sub as the routine is private of Form1... you may have to create a delegate in your module.
I have a form that loads just fine, and I'm trying to fire off a task using a Background Worker as it loads.
I'm getting no errors with the code below, but the bw.DoWork event doesn't seem to be firing.
Am I missing something here? Thanks.
Here is my form Class -
Public Class mainForm
Dim objWorker As MyWorker
Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call Me.loadForm()
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Call Me.closeForm()
End Sub
Private Sub loadForm()
Me.objWorker = New MyWorker ' Invoke the background worker
End Sub
Private Sub closeForm()
Me.objWorker.bw_Cancel() ' Cancel the background worker
Me.Close() ' Close the form
End Sub
End Class
Here is my BackgroundWorker Class -
Imports System.ComponentModel
Partial Public Class MyWorker
Private bw As BackgroundWorker = New BackgroundWorker
Public Sub New()
bw.WorkerReportsProgress = False
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
For i = 1 To 10
If bw.CancellationPending = True Then
e.Cancel = True
Exit For
Else
System.Threading.Thread.Sleep(500)
MsgBox("iteration " & i)
End If
Next
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
MsgBox("Complete!")
End Sub
Public Sub bw_Cancel()
If bw.WorkerSupportsCancellation = True Then
bw.CancelAsync()
End If
End Sub
End Class
add to MyWorker constructor ('new' method) this line:
bw.RunWorkerAsync()
I am stuck updating a progressbar from a different thread.
I did get it running in the simplest way, but then cleaning the code gets me stuck.
My testing code looks like all the examples on the web related to backgroundworker and BeginInvoke.
FormP is the Progressbar-Form.
This works:
Public Class Form1
Private Delegate Sub delegate_ProgressUpdate(ByVal paramValue As Integer,
ByVal paramMax As Integer)
Private Sub Button1_Click(sender As System.Object,
e As System.EventArgs) Handles Button1.Click
' Test 01:
' Show Progressbar via BGW
' All functions are within Form1
Dim bgw As New BackgroundWorker()
AddHandler bgw.DoWork, AddressOf BGW_Sample01_DoWork
FormP.Show(Me)
bgw.RunWorkerAsync()
End Sub
Private Sub invokeMe_ProgressUpdate(ByVal paramValue As Integer, ByVal paramMax As Integer)
FormP.ProgressBar1.Maximum = paramMax
FormP.ProgressBar1.Value = paramValue
FormP.ProgressBar1.Update()
End Sub
Private Sub BGW_Sample01_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
For i As Integer = 1 To 10
Threading.Thread.Sleep(500) ' Test delay
Me.BeginInvoke(New delegate_ProgressUpdate(AddressOf invokeMe_ProgressUpdate),
i, 10)
Next
MessageBox.Show("Fertig")
End Sub
If I try to make things work more orderly encapsulated in FormP, it doesn't work.
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim bgw As New BackgroundWorker
AddHandler bgw.DoWork, AddressOf BGW_Sample02_DoWork
FormP.Show(Me)
bgw.RunWorkerAsync()
End Sub
Private Sub BGW_Sample02_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
For i As Integer = 1 To 10
Threading.Thread.Sleep(500)
FormP.SetProgress(i, 10)
Next
MessageBox.Show("Fertig")
End Sub
' ########## FormP #################
Public Class FormP
Private Delegate Sub delegate_ProgressUpdate(ByVal value As Integer, ByVal maximum As Integer)
Public Sub SetProgress(ByVal paramValue As Integer, ByVal paramMaximum As Integer)
If Me.InvokeRequired Then
Me.Invoke(New delegate_ProgressUpdate(AddressOf Me.SetProgress), paramValue, paramMaximum)
Else
Me.ProgressBar1.Maximum = paramMaximum
Me.ProgressBar1.Value = paramValue
Me.ProgressBar1.Update()
End If
End Sub
End Class
FormP does not freeze, but UI is not updated.
Actually Me.InvokeRequired is false and I think that's where I begin to miss some important parts.
I tried Form1.InvokeRequired here, but it's false as well.
My understanding is: the calling thread here is the bgw thread, no matter in what class the code is that this thread calls...
That seems not to be it?
Thanks for any thoughts.
What worked eventually:
Private frmP As FormP
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim bgw As New BackgroundWorker
If Me.frmP IsNot Nothing AndAlso Me.frmP.Visible Then Return
Me.frmP = New FormP
Me.frmP.Show(Me)
AddHandler bgw.DoWork, AddressOf BGW_Sample02_DoWork
bgw.RunWorkerAsync(New Object() {Me.frmP})
End Sub
Private Sub BGW_Sample02_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
Dim objFrmP As FormP = DirectCast(e.Argument(0), FormP)
For i As Integer = 1 To 10
objFrmP.setProgress(i, 10)
Threading.Thread.Sleep(500)
Next
MessageBox.Show("Finished")
End Sub
The Progress-Dialog-Code in FormP:
Public Sub setProgress(paramValue As Integer, paramMaximum As Integer)
If Me.InvokeRequired Then
' defining a delegate type is not really necessary
Me.Invoke(Sub() Me.setProgress(paramValue, paramMaximum))
Else
Me.ProgressBar1.Maximum = paramMaximum
Me.ProgressBar1.Value = paramValue
Me.ProgressBar1.Update()
End If
End Sub