[RESOLVED] Pass data from BackgroundWorker to main thread?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] Pass data from BackgroundWorker to main thread?

  1. #1
    Join Date
    May 2010
    Posts
    11

    [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.

  2. #2
    Join Date
    May 2010
    Posts
    11

    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

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,771

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center