Solved

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

Posted on 2013-02-06
6
1,020 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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