Solved

background worker passing text to label and listview on main form .net visual basic

Posted on 2013-02-06
6
960 Views
Last Modified: 2013-02-14
I would like to move a process to a background worker.  I need it to pass 2 varibles to the background worker, have it run a process, then return back 2 variables.  How can I do that?

dim str1 ="test1"
dim str2=test3"

BackgroundWorker1.RunWorkerAsync()  'need to pass str1 and str2 to this

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
       

    End Sub 'return updated str1 and str2
0
Comment
Question by:chadmanvb
6 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 250 total points
ID: 38860509
Hi ..You need to use Delegates to avoid cross thread...
Declare initial the variables
Private VarBeforeSent as string
Private Delegate Sub SendTextDelegate(ByVal txt As String)
Private Sub SetVariable(ByVal StrVar As String)
        VarBeforeSent = StrVar
    End Sub

Open in new window


In the BackGroundWorker Event
Me.Invoke(New SendTextDelegate(AddressOf SetVariable), "Variable Value" )

Open in new window


Hope it Helps...John
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 38860541
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38860731
just for your label text change:
Private Sub SetVariable(ByVal StrVar As String)
        MyLabel.text = StrVar
    End Sub

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 38860801
Both good answers.  Sometimes I mix the two approaches together.

As demonstrated in Eric's article, you can pass in a parameter to the RunWorkerAsync() method.  If you have multiple values, then you can use a List(Of String), a simple array, or create a custom class to hold the values.  The important part here, though, is to cast "e.Argument" in the DoWork() handler back to whatever type you passed in so you can access the values within.

Similarly, you pass things OUT of the DoWork() handler by setting "e.Result" to something.  Again, you can pass out whatever type you want if you need to pass multiple items.  Just cast "e.Result" back to the correct type in the RunWorkerCompleted() event.

*The same thing can be done with ReportProgress() and the ProgressChanged() event.  There is an overload of ReportProgress() that accepts two parameters, with the second parameter being used to pass out information other than a simple integer for progress.  The "e.UserState" parameter in the ProgressChanged() event is then cast back to the correct type so you can access the values within.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38863609
>BackgroundWorker1.RunWorkerAsync()  'need to pass str1 and str2 to this
>End Sub 'return updated str1 and str2

Approaches described above are proper approaches but given that these are variables not controls on UI thread and you need to set them BEFORE calling worker and read them AFTER worker has completed, you can simply declare these as class level variables. Set these before calling the worker and read them back after worker has completed.
0
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 250 total points
ID: 38864125
Create a new form and a background worker, the following code should work for you:
Public Class Form1
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        MsgBox("Running")
        e.Result = e.Argument
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Dim objResult() As Object = e.Result
        MsgBox(objResult(0))
        MsgBox(objResult(1))
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str1 As String = "String1"
        Dim str2 As String = "String2"
        Dim obj() As Object = {str1, str2}
        BackgroundWorker1.RunWorkerAsync(obj)
    End Sub
End Class

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now