C# MultiThreading.

What is a Thread?
Every application runs in one or more “Threads of execution”. In a single threaded application if you had:
DoSomething1();
DoSomething2();
It would complete DoSomething1() before even starting on DoSomething2() right? Right. If you could run both things at the same time you could theoretically (assuming both task takes the same amount of time to complete) cut the processing time in half. Allot of you that have eitherused Delphi or C# have used the “timer” components, that allow you to run more than one section of code simultaneously and independently of the main code… that is multithreading.

Why use Threads?

  • Parallel processing.
  • Avoiding freezing up GUI’s
  • Shorter processing times

Etc
A quick example is recently I had a function that ran with a constant while loop that would, if on the same thread as the GUI, freeze up the GUI – now this would have been really inappropriate because the GUI had to control the while loop…. So I started the process in a secondary

thread at problem solved.

NOTE:
You have to include “using System.Threading”.

Threading…:

The below code demonstrates how to start and create threads:

static void Thread2()
{
for (; ; )
{
Thread.Sleep(500); //simulate large calculation
Console.WriteLine(“Thread 2”);
}
}

static void Thread1()
{
for (; ; )
{
Thread.Sleep(500); //simulate large calculation
Console.WriteLine(“Thread 1”);
}
}

static void Main(string[] args)
{
Thread t1 = new Thread(Thread1);
Thread t2 = new Thread(Thread2);
t1.Start();
t2.Start();
}

The threads run almost at the same time in instance each with the same delay. But what happens when your threads are not synchronized?
What happens when Thread1 requires a result from Thread2 but thread one takes 1/4 of the time that thread2 takes to execute?
To demonstrate just how UNSYNCHRONISED the above code is have a look at the below screenshot.

As you can see at the bottom thread2 executes before thread1 effectively knocking what ever pseudo-synchronization out.

Synchronization:

Luckily there are a few methods of “controlling” threads:
The LOCK statement is probably the most commonly used but don’t let it lure you into a false sense of security.
Locks ensure that the “locked” block of code runs until completion without interruption from other threads.
It is generally accepted as a rule NOT to lock onto public objects (such as lock (this) ) because if the object
can be publicly accessed other locks may be accidentally or unintentionally emplaced creating bottlenecks and freezes.

The below code demonstrates how to use LOCKS:

private static Object ObjLock = new Object();

static void Thread2()
{
for (; ; )
{
lock (ObjLock)
{
Thread.Sleep(500); //Simulate large calculation
Console.WriteLine(“Thread 2”);
}//End of locked code
}
}

static void Thread1()
{

for (; ; )
{
lock (ObjLock)
{
Thread.Sleep(500); //Simulate large calculation
Console.WriteLine(“Thread 1”);
}//End of locked code
}
}

static void Main(string[] args)
{
Thread t1 = new Thread(Thread1);
Thread t2 = new Thread(Thread2);
t1.Start();
t2.Start();
}

Advertisements

~ by Rhys Mossom on March 10, 2007.

5 Responses to “C# MultiThreading.”

  1. Hello!
    How are you?

  2. I’m good thanks… and you?

  3. yo Rhys! Its Tyler here. I need some help =/

    I am making a GUI that kills/launches processes at selected times. And i can’t multi thread >=(

    It requires an object reference for a non static field, method, or property =/

    Any idea’s?
    Hit me up on msn!

  4. Dude youre not on msn 😐
    just send me a message as soon as you get on or something…

  5. I am here at a forum newcomer. Until I read and deal with the forum.
    Let’s learn!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: