Solved

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

Posted on 2013-02-06
6
1,030 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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