There is a number of aspects of passing data between threads. First important aspect is passing notifications from a non-UI thread to, specifically, to a UI thread. You cannot call anything related to UI from non-UI thread. Instead, 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:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.
See also the reference in my other answer:
.NET event on main thread[
^].
Note the reference above related to
blocking collection (
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[
^]). The invocation mechanism I explained above is implemented only for the UI frameworks/libraries. It is inapplicable to some arbitrary threads; you would need to create a similar mechanism. My article of blocking collection explains it, and an alternative solution to mine recommends existing .NET BCL class
System.Collections.Concurrent.BlockingCollection
.
Other aspects of passing data in asynchronous way, especially at thread initialization (but not only), are covered by the technique of
thread wrappers I put forward. Please see my past answers:
Making Code Thread Safe[
^],
How to pass ref parameter to the thread[
^],
Change parameters of thread (producer) after it is started[
^],
MultiThreading in C#[
^].
—SA