Solved

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

Posted on 2013-02-06
6
939 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
Comment Utility
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
Comment Utility
0
 
LVL 18

Expert Comment

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
>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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

11 Experts available now in Live!

Get 1:1 Help Now