Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 24-Apr-12 13:38pm
amramaz130

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
amramaz at 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.
SAKryukov at 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)

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 27 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100