473,396 Members | 2,011 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,396 software developers and data experts.

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

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
Nov 19 '08 #1
2 1786
On 2008-11-19, cj2 <cj*@nospam.nospamwrote:
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
--
Tom Shelton
Nov 20 '08 #2
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/libr....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/keyde...amming-Using-B
ackgroundWorker/.

Have a nice day.

Regards,
Hongye Sun (ho*****@online.microsoft.com, 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:
ms****@microsoft.com.
*
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '08 #3

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

Similar topics

2
by: rob | last post by:
Dear All, I am getting an error "Illegal cross-thread operation". I am not creating any threads. I know internally there are many threads running, though. Does that mean that no form can call a...
5
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
1
by: Martin Schmid | last post by:
I've got this simple method to process images.. .however, the Refresh doesn't seem to work... also, since it is looping over potentially many files, I'd like to be able to add a 'Stop' button to...
13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
9
by: Víctor | last post by:
Hello, I'm filling a array of System.Diagnostic.Process by using GetProcesses() method. Due to a retard on this method, I do the call using a function and it passing like a delegate to one...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
4
by: RSH | last post by:
Hi, I have an Asynchronus process that is reporting back to the UI at regular intervals. I read several posts on how to use delegates to make this happen. But for some reason my application...
11
by: winningElevent | last post by:
This code C# is from online source but it was in console application so I modify to work on windows form just for learning on how to use threading.timer, but it doesn't work. Can anyone help me? ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.