how do I get a thread to update a form1.richtextbox1.text?

C

cj2

How do I get the threads to update the richtextbox1 on form1? This
isn't working.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
For x = 1 To 7
Dim oSession As SessionClass
Dim oThread As Thread

Try
oSession = New SessionClass
oThread = New Thread(AddressOf oSession.ThreadMain)
oThread.ApartmentState = ApartmentState.STA
oThread.Start()
Catch ex As Exception
MessageBox.Show("starting thread " & Str(x) & " failed")
End Try
Next
MessageBox.Show("end test")
End Sub


Imports System.Threading
Module Module1
Public Class SessionClass
Public Sub ThreadMain()
MyThreadCount.Increment()
System.Threading.Thread.Sleep(1000)
Form1.RichTextBox1.AppendText(vbCrLf &
MyThreadCount.ThreadCount)
MyThreadCount.Decrement()
End Sub
End Class
end module
 
T

Tom Shelton

How do I get the threads to update the richtextbox1 on form1? This
isn't working.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
For x = 1 To 7
Dim oSession As SessionClass
Dim oThread As Thread

Try
oSession = New SessionClass
oThread = New Thread(AddressOf oSession.ThreadMain)
oThread.ApartmentState = ApartmentState.STA
oThread.Start()
Catch ex As Exception
MessageBox.Show("starting thread " & Str(x) & " failed")
End Try
Next
MessageBox.Show("end test")
End Sub


Imports System.Threading
Module Module1
Public Class SessionClass
Public Sub ThreadMain()
MyThreadCount.Increment()
System.Threading.Thread.Sleep(1000)
Form1.RichTextBox1.AppendText(vbCrLf &
MyThreadCount.ThreadCount)
MyThreadCount.Decrement()
End Sub
End Class
end module

First, if you are in a Windows forms application, the easiest way is to use
the backgroundworker component and handle it's ProgressChanged event. This
will eliminate a lot of complexity if all you need is a single thread (like
your example).

If you are doing more complext stuff, then you need to basically use a
delegate to marshal the calls back to the controls thread. See here:

http://msdn.microsoft.com/en-us/library/ms951089.aspx

That's part one of a 3 part article. The sample code is C#, but the concepts
still apply to vb.net.

HTH
 
H

Hongye Sun [MSFT]

Thanks HTH, your answer is accurate and clear.

Hi Tom,

Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure to
work with you and HTH.

HTH has already answered your question. For your situation, the best way is
using BackgroudWorker
(http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundwor
ker.aspx). In addition, to save your time, I provide a step by step demo
video of how to use BackgroudWorker. You can find it at
http://channel9.msdn.com/posts/keydet/Easy-Multithreaded-Programming-Using-B
ackgroundWorker/.

Have a nice day.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hongye Sun [MSFT]

Hi Chris,

I have not heard from for several days. Have you had a try of
backgroundworker? I am writing to follow up this issue and check if the
issue has been well resolved. Do you mind letting us know the current
status of the issue? We will be more than happy to be of further
assistance. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hongye Sun [MSFT]

Thanks for your reply, Chris.

Please feel free to let us know if you have problem when you have time to
try the solution. We will be more than happy to help you. Thanks.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top