In this code snippet, we will see how to exchange thread data by using an object in C#.
This post is very similar to this post I already made about sending/receiving data to/from a thread. The only difference is that here an object will be used for the data.
To pass data into the thread we will create a class and make two properties one for the input data and one for the callback delegate. When an instance of the class is being created the input data and the callback delegate must be passed into the constructor. After the object has been created its doWork() method can be started as a new thread.
To get data out of the thread displayResult() will be called when doWork() is done executing. The result data will be passed as an input parameter to displayResult().
Let’s have a look at the code below to see how to exchange thread data by using an object.