473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Paramters passing and RunWorkerCompleted event

I am trying to use the example at:
http://msdn2.microsoft.com/en-us/lib...28(d=ide).aspx
to update a control in a thread safe maner.

The article shows a couple of ways to do so and indicates using the
RunWorkerCompleted() event handler is the preferred. However, the
example they have does not show how to pass a parameter to use, but
instead uses a hardcoded value:

' BackgroundWorker is the preferred way to perform asynchronous
' operations.
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

How can I use this method with a passed in value?

Thanks.

Jun 22 '06 #1
3 7890
On 22 Jun 2006 11:27:10 -0700, nt*******@sneakemail.com wrote:
I am trying to use the example at:
http://msdn2.microsoft.com/en-us/lib...28(d=ide).aspx
to update a control in a thread safe maner.

The article shows a couple of ways to do so and indicates using the
RunWorkerCompleted() event handler is the preferred. However, the
example they have does not show how to pass a parameter to use, but
instead uses a hardcoded value:

' BackgroundWorker is the preferred way to perform asynchronous
' operations.
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

How can I use this method with a passed in value?

Thanks.


I think you are misreading the example. The example demo's unsafe and
safe methods. The code you included is only a piece of the safe
method which is as advertised - RunWorkerComplete:

You need all of this:

' This event handler creates a thread that calls a
' Windows Forms control in a thread-safe way.
Private Sub setTextSafeBtn_Click( _
ByVal sender As Object, _
ByVal e As EventArgs) Handles setTextSafeBtn.Click

Me.demoThread = New Thread( _
New ThreadStart(AddressOf Me.ThreadProcSafe))

Me.demoThread.Start()
End Sub
' This method is executed on the worker thread and makes
' a thread-safe call on the TextBox control.
Private Sub ThreadProcSafe()
Me.SetText("This text was set safely.")
End Sub

' This method demonstrates a pattern for making thread-safe
' calls on a Windows Forms control.
'
' If the calling thread is different from the thread that
' created the TextBox control, this method creates a
' SetTextCallback and calls itself asynchronously using the
' Invoke method.
'
' If the calling thread is the same as the thread that created
' the TextBox control, the Text property is set directly.

Private Sub SetText(ByVal [text] As String)

' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.textBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.textBox1.Text = [text]
End If
End Sub

' This event handler starts the form's
' BackgroundWorker by calling RunWorkerAsync.
'
' The Text property of the TextBox control is set
' when the BackgroundWorker raises the RunWorkerCompleted
' event.
Private Sub setTextBackgroundWorkerBtn_Click( _
ByVal sender As Object, _
ByVal e As EventArgs) Handles setTextBackgroundWorkerBtn.Click
Me.backgroundWorker1.RunWorkerAsync()
End Sub
' This event handler sets the Text property of the TextBox
' control. It is called on the thread that created the
' TextBox control, so the call is thread-safe.
'
' BackgroundWorker is the preferred way to perform asynchronous
' operations.
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
Gene
Jun 22 '06 #2

gene kelley wrote:
On 22 Jun 2006 11:27:10 -0700, nt*******@sneakemail.com wrote:
I am trying to use the example at:
http://msdn2.microsoft.com/en-us/lib...28(d=ide).aspx
to update a control in a thread safe manner.

The article shows a couple of ways to do so and indicates using the
RunWorkerCompleted() event handler is the preferred. However, the
example they have does not show how to pass a parameter to use, but
instead uses a hardcoded value:

' BackgroundWorker is the preferred way to perform asynchronous
' operations.
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

How can I use this method with a passed in value?

Thanks.


I think you are misreading the example. The example demo's unsafe and
safe methods. The code you included is only a piece of the safe
method which is as advertised - RunWorkerComplete:

Gene,

I think you missed my question. Yes I know you need all of the code, I
was simply pointing to the code I had a question about.

Let me try and restate it since it was not obvious.

I would like to pass a variable parameter to the routine that is going
to update the control. In the example, there is no way to do this as
the value going to the control is static. ie:"This text was set safely
by BackgroundWorker."

My question is how can I use this preferred thread safe method and pass
in the value to be sent to the control?

Thanks.

Jun 23 '06 #3
I would like to pass a variable parameter to the routine that is going
to update the control. In the example, there is no way to do this as
the value going to the control is static. ie:"This text was set safely
by BackgroundWorker."

My question is how can I use this preferred thread safe method and pass
in the value to be sent to the control?


In DoWork event handler, set the Result property in the passed in
DoWorkEventArgs. You can then get that value from the Result property of the
RunWorkerCompletedEventArgs in the RunWorkerCompleted event handler. You can
also pass a parameter to DoWork by using the overload of RunWorkerAsync and
then use the Argument property of the DoWorkEventArgs to access it.
worker.RunWorkerAsync("My Argument")
.....
Private Sub worker_DoWork(ByVal sender As System.Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
Debug.WriteLine(e.Argument.ToString())
e.Result = "My Result"
End Sub

Private Sub worker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e
As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
worker.RunWorkerCompleted
Debug.WriteLine(e.Result.ToString())
End Sub
/claes
Jun 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
2
by: JWH035 | last post by:
I'm having a problem properly passing a datetime variable to SQL from C#. In the sqladapter's properties / SelectCommand / parameters when I hard code the Value to a date for selection purposes the...
9
by: Jacob | last post by:
Is there any way (maybe using an attribute) that I can force strings or other reference parameters to always have an instance even if they are blank? Ex: public string DoSomething(string Text)...
0
by: genojoe | last post by:
I am running an application that, when not used, just sits there firing a BackgroundWorker every 20 seconds. Every now and then, the BackgroundWorker freezes between the DoWork and...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
by: bwaichu | last post by:
In some APIs, we see char ** or void ** as a parameter. I never distinguished between declaring a variable as char **x and passing x as the parameter from declaring a variable char *x and passing...
1
by: VAADADMIN | last post by:
I have a small app that I am working with to create LDIF files from text files. I have a pictureBox that has an animated GIF that will appear on the form when the LDIF are being created. The...
1
by: DrLargePants | last post by:
If I have something like below, and I catch an error in DoWork, how do I then get e.Error in RunWorkerCompleted to populate ? Private Sub bg_DoWork(ByVal sender As Object, ByVal e As...
3
by: =?Utf-8?B?d2lsa29rb3N0ZW4=?= | last post by:
Dear all, I have the following question: I want to populate a datagridview after my BackGroundWorker has finished. I thought i could use the RunWorkerCompleted event to update my datagridview...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.