Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are having to thread running in parallel now how to pass a value from one thread to another thread.Please help me out.
Posted

There can be many different scenarios of inter-thread synchronization and passing the data. One of the most widely used patterns is using data queues, in particular, blocking queues; the blocking enables synchronization of the thread expecting data with the availability of the data supplied form other thread(s). On interesting variant of it is using delegate instances, so one thread can pass the whole method with all required parameters (importantly, including "this") to be executed on another thread.

This creates a mechanism similar to the UI thread invocation mechanism (implemented for System.Windows.Forms and WPF with System.Windows.Forms.Control.Invoke (BeginInvoke) or System.Windows.Threading.Dispatcher). But — attention! — such invocation is only available for UI threads through respective UI libraries; for the arbitrary threads, the similar mechanism can be created by a developer, which I demonstrate in my article referenced below.

For further detail, please see my article complete with full source code and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

See also the alternative solution to this article which is applicable to .NET Framework 4.0 and later.

—SA
 
Share this answer
 
v4
Comments
BillW33 5-Sep-12 14:08pm    
Very fine answer and tip; +5
Sergey Alexandrovich Kryukov 5-Sep-12 14:10pm    
Thank you very much,
--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