Click here to Skip to main content
15,867,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we modify the thread operation by using another thread.like i want to change the label text which is updating in a thread operation,but i want to change the text of label by another thread without disturbing the original operation.for example in first thread label text incremented by one and when it reaches to 9 it again becomes 0,but when i run my second thread i want to increment the label text by 2 and maximum range is 100.Thanks
Posted
Updated 26-Jul-14 10:02am
v2
Comments
Andreas Gieriet 26-Jul-14 16:32pm    
Show us what you have done so far and what problem you are facing. This is not a forum where you get the code for your problems. It's a quick answer forum that helps with concrete issues.
Cheers
Andi

You need to first understand what the GUI thread is. Then you need to understand how to update (from another thread) GUI elements that were created on the GUI thread. Finally, you might need to get acquainted with how to synchronize between threads to avoid races between the thread actions.
This looks very much like a homework assignment. Therefore, I let you look up the respective information yourself.
Cheers
Andi
 
Share this answer
 
The question makes no sense just because you cannot change the label text from any thread except the UI thread. What is the UI thread? It relation to your label object, this is the thread which called Application.Run with the form which created all the controls owning your label. You can modify the label text (or anything at all) only in the same very thread. To trigger such change in any other thread, you can use the mechanism of invocation which is implemented for UI thread for both WPF and System.Windows.Forms.

Instead of trying to modify any UI properties directly, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

If you want to delegate some actions to some thread which is not a UI thread, you will have to implement a similar synchronization and data passing mechanism by yourself. Please see my article where it is explained in detail: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900