In this code snippet, we will see how to pass/get data to/from a thread in C#.
To get the data from the thread a delegate callback method will be used. First, we will make a regular method that takes in an input parameter. Then we will make a delegate and make it point to this method. Finally, the delegate will be passed as an input parameter to the method that is being started as a new thread. When the child thread is done executing it will call the callback method that is located in the main thread and pass it the result as an input parameter. This is how we will be able to get the result from the child thread back into the main one.
See this post if you want to know how to exchange data with a thread by using an object. It is very similar to what we are doing here.
Let’s have a look at the code below to see how to pass/get data to/from a thread.