C# Thread Data Exchange by Object

C# Code Snippets Thread Data Exchange by Object
Share:

About

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.

Code:

Resulting output:

Share:

Leave a Reply

Your email address will not be published.

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger