-
April 23rd, 2013, 05:58 AM
#1
[RESOLVED] Pass data from BackgroundWorker to main thread?
Hello
In a Windows Form, I need to delegate a lengthy action to a BackgroundWorker so that the main application doesn't freeze.
However, the code in the BackgroundWorker must send data back to the main application. This code from MSDN doesn't include how to do this:
Code:
Private Sub setTextBackgroundWorkerBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles setTextBackgroundWorkerBtn.Click
Me.backgroundWorker1.RunWorkerAsync()
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
Me.textBox1.Text = "This text was set safely by BackgroundWorker."
End Sub
Is there a way for a BackgroundWorker object to send data back to the main app without using a global variable?
Thank you.
-
April 23rd, 2013, 06:44 AM
#2
Re: Pass data from BackgroundWorker to main thread?
For others' benefit, here's some working code:
Code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Lengthy process here
e.Result = "Blah"
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.RichTextBox1.AppendText(e.Result)
End Sub
-
May 3rd, 2013, 01:23 AM
#3
Re: [RESOLVED] Pass data from BackgroundWorker to main thread?
If you need more than just the final result, or regular updates from the worker thread you could also use Delegate and Invoke..
Code:
Delegate Sub InvokeDelegText(ByVal value As String)
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Lengthy process here
If Progress.LblProgress.InvokeRequired Then
' It's on a different thread, so use Invoke.
Progress.LblProgress.BeginInvoke(New InvokeDelegText(AddressOf SetText), text)
End If
'Lengthy process here
If Progress.LblProgress.InvokeRequired Then
' It's on a different thread, so use Invoke.
Progress.LblProgress.BeginInvoke(New InvokeDelegText(AddressOf SetText), text)
End If
End Sub
Private Sub SetText(ByVal Text As String)
Progress.LblProgress.Text = Text
End Sub
This code is very handy for passing any amount of data between threads.. I use it now extensively in many Multi threaded applications..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|