Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two threads : t1 and t2.
t1 is the main thread, and it has created the t2 thread.
After creating t2, t1 continue doing some work. on t2, there is at some point a code that I want to be executed on the t1 thread, while t2 continue to the rest of his code. is there a way that t2 can tell t1 "stop what you'r doing now, run this code, and than return to do your work " ?
I know that this is possible when you want to run code on the UI Thread in Windows Form Application,using Control.Invoke() or SynchronizationContext class, but is there any regular threads way ?
Posted

1 solution

Of course you can invoke a method on the other thread, more exactly, a delegate instance, but…

Your "stop what you're doing now, run this code, and than return to do your work" sounds like thread preemption. Do you mean, t1 should be agnostic to the code to be executed when it is to be interrupted? — it sounds like. If so, not that this is impossible, it cannot be attributed any meaning. Let's think about that. Preemption of a thread happens all the time — this is how threading works. Thread switching can be cooperative or can happen on hardware interrupts, and in all cases a thread can be preempted, its execution is interrupted, its context is saved, and some other code is executed. After a while, the same thread can be scheduled for execution again. From the perspective of this thread, nothing happened — execution is continued from the same point in the same environment. Basically, a thread "feels" like it is being executed along. But what code is running when a thread is switched off? Anything but this thread, by definition. Interrupt procedure is executed, eventually a thread scheduler, anything in the system core needed to maintain threading, and other threads. Just think about it — this is the heart of the idea of threading. When a thread is preempted, what is being executed at that time is not this thread.

Now, don't be fooled by the idea of thread invocation. It simply means that one thread feeds some delegate instanced to some other thread specially designed to accept those instances and invoke them. An example of such thread is a UI thread, either System.Windows.Forms or WPF, but in principle it can be anything else. Keep reading; and I'll explain how it works. First, to get an idea on how invocation works on the UI threads, please read my past answers on this topic:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Many developers have been confused by the fact the the Invoke/BeginInvoke methods always works somehow, even if there is no UI thread. This is nothing more than the fool-proof design of the System.Windows.Threading.Dispatcher (please see http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^]). It is designed to invoke "dispatched" delegate instance in all cases, but in can be trivially invoked on the same thread, as simple as that.

To use inter-thread invocation to a thread other then a UI thread, you need to specially design that thread to accept those delegate instances and invoke them. In particular, that thread needs to have a main processing loop and a queue where other threads can feed the delegate instances. If you need to understand how it works, please read my small article on this topic, complete with full source code and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 
v2
Comments
amramaz 29-Apr-12 4:54am    
Thank you very mach for the detailed answer. you made it clear to me, and I also know now what is the different between invoke and beginInvoke. Yet, creating a blocking queue is a little bit hard and involves writing some extra code. I am disappointed that there is now short way like calling a command for doing that, but at least I know how to do it.
Sergey Alexandrovich Kryukov 30-Apr-12 19:43pm    
There is no a short way for a purpose, due to nature of threads, which is very productive way of programming. If someone made threading different, think about it: would you have all that freedom you have with thread now? I don't think there is a reason for disappointment. Thinks just work a bit different from what you might have thought.

Now, the queue is not hard, partly because I provide complete and reliable source code, and partly because v.4.0 offers the similar queue already -- please see the alternative solution to my article.

Good luck, call again.
--SA

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