Parallel execution of code C#

Introduction to Thread

Multithreading is solution to parallel execution of code in C#. A thread is an independent execution path. Thread can run simultaneously with other threads. To use this Technic in C# you have to add namespaces call:

using System;

using System.Threading;

lets look at simple example:

class FirstThread
{
static void Main ()
{
Thread thisThread = new Thread (writeThread);
thisThread.start();
for (int i = 0; i<100; i++)
consol.write (“First”) ;
}

static void writeThread ()
{
for (int i = 0; i<100; i++)
consol.write (“Thread”);
}

}

Main thread will create new thread call writeThread, wile Simultaneously the main thread do its Job and print “First”.

Once you run the code, IsAlive  property will set to true, and after thread finish a value will set false. Take note that once thread ended you can’t restart it.

 

 

2 Comments

Leave a Reply