How to Multithread in C#


This WinForms C# Application will show you the basics of creating and using a thread and a bit more. It will actually show you multiple threads working simultaneously. As you know that threads are meant to do multiple jobs at a time by utilizing the different logical cores of the CPU, we will use this to work the value of multiple Int64 variables. See the application screenshot when the application starts and look for the source code at the end:




What we want in our above application is to created two Int64 variables and simply increase their values using two different threads. When we click the START button, the thread initializes and the counting process starts. The value of the variable increases continuously in the background. When we click the Show Current Value button, the value currently stored in the Int64 variable is shown in the TextBox. You may click the Show Current Value button as many times as you want. Everytime you get an incremented value that we have managed to get via an Infinite Loop.

Lets start by adding the code to create the variables in the program:


public Int64 FirstInt = new Int64();

public Int64 SecondInt = new Int64();

As you might have guessed the FirstInt and SecondInt are the variables that we will work with. These are defined at the start of the code outside the scope of all functions so that they are visible to every function.

Next we create the two functions that will work on the two different threads. These functions will start to increase the value of the two variables continuously using an infinite loop. To create an Infinite Loop, simple leave the middle section of the loop blank. i.e. There will be no check applied during the iteration process so that the loop never checks for a value to stop itself. Watch the code below:


private void StartTextCount1()
        {
            for (int m = 0;   ; m++)
            {
                FirstInt++;
            }
        }

private void StartTextCount2()
        {
            for (int n = 0;   ; n++)
            {
                SecondInt++;
            }
        }


The next thing we need to assure is that we have initialized the value of our variables. Because incrementing the values of an uninitialized variable may result in an error or may give garbage values. So in the Load() event of the form we set the value of the variables to 0 each. See code below:

private void Form1_Load(object sender, EventArgs e)
        {
            FirstInt = 0;
            SecondInt = 0;
        }

Now that all values have been set up and the form loaded, we need the simple code to create a new thread when the first start button that is above of two is clicked. See code below to create a new thread:

private void Start1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread Thread1 =
        new System.Threading.Thread(StartTextCount1);

            Thread1.Start();
        }

The thread created is called Thread1. This thread runs the StartTextCount() function. The thread requires the parameter that is the function that it will work on. When we call the Start() method of the Thread, the method in the parameter is called via the thread. Similarly write code for the second button to create the second thread:

private void Start2_Click(object sender, EventArgs e)
        {
            System.Threading.Thread Thread2 =
        new System.Threading.Thread(StartTextCount2);

            Thread2.Start();
        }

The last thing that remains is to create the code to show the variable value into the TextBox. This code below converts the Int64 value of the variable into a String. Then it displays it in the Black background TextBox called Text1. The same code applies for the second TextBox named Text2.

private void ShowValue1_Click(object sender, EventArgs e)
        {
            Text1.Text = FirstInt.ToString();
        }


private void ShowValue2_Click(object sender, EventArgs e)
        {
            Text2.Text = SecondInt.ToString();
        }


The output screenshots below were taken at different intervals:





Directly Download Source Code in Visual Studio 2012 Here.
Custom Search