Monday, August 28, 2017

The First Multithreading Program in C# to learn as a Beginner

I will teach you how to start with Multithreading in C#. Here I will build a simple program that uses Multithreading. The program starts with an entry point which is executed by the first thread and then calls a function which is executed by second thread.

Since this is a multithreading example therefore the 2nd thread will work asynchronously.


Points to Note:

1. The function will be called by a delegate. I will use the delegate’s ‘BeginInvoke method to call the function asynchronously. To receive the returned value from the function, I will use the delegate’s ‘EndInvoke’ method.

2. I will make use of ‘IAsyncResult’ interface which helps in making asynchronous operations.

The function to be called by the Delegate:

The function that will be called by the delegate is an addition function that takes 2 int values and returns the sum of them.

static int Add(int x, int y)
{
    int tid = Thread.CurrentThread.ManagedThreadId; //second thread id
    Thread.Sleep(10000);
    return x + y;
}

The Add function will be called by the 2nd thread asynchronously. The ‘tid’ variable will provide you the 2nd thread id  which will be different than that of the thread that executes the entry point.

The Entry Point:

The entry point will be a small code where I will call the ‘Add’ function through a delegate in asynchronous manner.

This is how to proceed:

a. Import the namespaces

using System.Threading;
using System;

b. Create the delegate

delegate int BinaryOp(int x, int y);

c. Entry Point Code

/*Invoke Add() on a secondary thread.*/
BinaryOp b = Add;
IAsyncResult iftAR = b.BeginInvoke(10, 20, null, null);

while (!iftAR.IsCompleted)
{
    int tid = Thread.CurrentThread.ManagedThreadId; //first thread id
    Thread.Sleep(1000);
}
// Obtain the result of the Add() method when ready.
int answer = b.EndInvoke(iftAR); // 30
/*End*/

Explanation:

First I created the delegate’s object and associated the ‘Add’ function to it.  

Then I created an object of ‘IAsyncResult’ to call the delegate’s ‘BeginInvoke’ method. I passed the 2 numbers to the first two parameters and null to the last 2 parameters.

The second thread will come into existence and will execute the ‘Add’ function.

 I have use ‘iftAR.IsCompleted’ to check when the asynchronous request (the 2nd thread returns the sum of the 2 numbers) is completed. The ‘iftAR.IsCompleted’ returns true when the asynchronous request gets completed. If it returns false then I make my first thread to sleep for 1 second.

Due to the 10 seconds sleep on the ‘Add’ method, the add method will take slightly more than 10 seconds to execute. In the meanwhile the first thread waits (see the while block).

The first thread Id which you will get on the while block will be different that the seconds thread id which you will get on the ‘Add’ function. This signifies that there are 2 threads doing our work.
The int variable ‘answer’ will return 30.

Conclusion


I hope you leaned the first Multithreading program code in this tutorial. It is a very powerful feature of C# and can be used to build high performing codes in your applications.

No comments:

Post a Comment

How to use SyntaxHighlighter in your website

SyntaxHighlighter is a JavaScript code that highlights programming languages codes in different colors and fonts. This helps to understan...