C# Sending and Receiving Data From a Thread

C# Code Snippets Sending and Receiving Data From a Thread
Share:

About

In this code snippet, we will see how to pass/get data to/from a thread in C#.

To pass data into the thread we will simply pass it as an input parameter to the method that is being started as a new thread.

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.

Note: In most cases, the best way to work with threads is by using the TPL(Task Parallel Library). Consider using it instead of working with threads directly like shown in this post.

Let’s have a look at the code below to see how to pass/get data to/from a thread.

Code:

using System;
using System.Threading;

namespace sendingAndReceivingDataFromAThread
{
    class Program
    {
        static void Main(string[] args)
        {
            //This delegate will be called when the thread is done executing.
            doWorkCallback callback = new doWorkCallback(displayWorkDone);

            int threadInputData = 5;

            //doWork() runs as a separate thread. We pass it the data and the callback delegate.
            Thread workThread = new Thread(() => doWork(threadInputData, callback));

            //Run thread.
            workThread.Start();

            Console.ReadLine();
        }

        public delegate void doWorkCallback(int result);

        public static void displayWorkDone(int result)
        {
            Console.WriteLine("Result: " + result);
        }

        static void doWork(int n, doWorkCallback callback)
        {
            int result = 0;

            for (int i = 0; i < n; i++)
            {
                //Do some work....
                Thread.Sleep(1000);
                result += 10;
            }

            //Call the callback delegate which points to the displayWorkDone() method and pass it the result to be returned from the thread.
            callback(result);
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

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