This is an Easy Q, but great help:
Under title’ Retrieving data from threads MSDN (Here) introduces a way to get data from a child thread by using callback method which is encapsulated by a delegate passed from main thread to the child thread – who has the data.
You can see that clearly ( the last Example in the MSDN page)
My Q is, since we are taking about retrieving data (from child threads to main thread) the call back method should be executed by the main thread and not by the child thread…
I changed the code a bit ( to verify that) so I attached the name of the thread before each output:
public static void ResultCallback(int lineCount) { Console.WriteLine(Thread.CurrentThread.Name + ':Independent task printed {0} lines.', lineCount); }
And I named the child thread: ‘Method2’ while the main thread ‘System’…
I got this output: Method2: Independent task printed 1 lines.
where the correct output should have been : System:Independent task printed 1 lines..
Who is drunk here? MSDN, me or .NET?
‘You’ ;-p
Callbacks are still executed on the worker thread; they provide the data back to the calling code – but they don’t have an inbuilt way of interrupting the calling thread. It is your code’s responsibility to push that work back to the main thread if necessary. For example, in a winforms app you might have: