Click here to Skip to main content
Click here to Skip to main content

Multithreaded Programming Using C#

By , 10 Oct 2001
 

Introduction

Threading is a lightweight process. With the help of threads we can increase the response time of the application. To use multithreading we have to use the Threading namespace which is included in System. The System.Threading namespace includes everything we need for multi threading. Now lets see the first program.

Program 1

using System;
using System.Threading;

public class MyThread {

        public static void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public static void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1 ) );
                Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2 ) );

                tid1.Start();
                tid2.Start();
        }
}

Let's explore the the whole program.

This program has a class MyThread which has two static functions Thread1 and Thread2. To make a thread you have to make an object of class Thread. The constructor of this class takes a reference of a ThreadStart class. This constructor can send two types of exceptions; ArgumentNullException when the parameter is a null reference or a Security Exception when program does not have permission to create thread.

The parameter of the Thread class is reference to a ThreadStart class. ThreadStart class points to the method that should be executed first when a thread is started. The parameter is the name of the function, which is considered as a thread function. Thread1 is a static function so we give it with the name of class name without making an object of the class. The thread starts execution with Start() method of the Thread class. The output of this program is

Before start thread
Thread1 0
Thread1 1
Thread1 2
Thread1 3
Thread1 4
Thread1 5
Thread1 6
Thread1 7
Thread1 8
Thread1 9
Thread2 0
Thread2 1
Thread2 2
Thread2 3
Thread2 4
Thread2 5
Thread2 6
Thread2 7
Thread2 8
Thread2 9

It is not a requirement that thread function must be static. We can make it a non-static function but in this case we have to create object of that class.

Program 2

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread1 {0}", i);
                }
        }

        public void Thread2() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Thread2 {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

		MyThread thr = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr.Thread2) );

                tid1.Start();
                tid2.Start();
        }
}

The output of this program is same as previous one.

It is not necessary to make two functions for making two threads. You may have only one thread function and create two threads by creating two objects of the thread class. Take a look at this program.

Program 3

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Here we create two objects of class MyThread to execute two threads.

It is also not necessary to create the object of ThreadStart at the time of passing paramaters in the constructor. You can create the object of ThreadStart and pass it as a parameter of Thread. Let's see this program.

Program 4

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world {0}", i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

		MyThread thr1 = new MyThread();
		MyThread thr2 = new MyThread();

		ThreadStart tStart1 = new ThreadStart(thr1.Thread1);
		ThreadStart tStart2 = new ThreadStart(thr2.Thread1);

                Thread tid1 = new Thread(tStart1);
                Thread tid2 = new Thread(tStart2);

                tid1.Start();
                tid2.Start();
        }
}

This program also gives the same output.

The Sleep method suspends the execution of the thread for a specified time. Sleep is a static method so it can be used without making an object of Thread. There are two overloaded versions of Sleep(), one function takes time in milliseconds and second method take a reference of an instance of the TimeSpan structure.

Program 5

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {
                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Now the thread sleeps for one millisecond and gives the second thread an opportunity to execute. The output of this program is

Before start thread
Hello world 0
Hello world 0
Hello world 1
Hello world 1
Hello world 2
Hello world 2
Hello world 3
Hello world 3
Hello world 4
Hello world 4
Hello world 5
Hello world 5
Hello world 6
Hello world 6
Hello world 7
Hello world 7
Hello world 8
Hello world 8
Hello world 9
Hello world 9

Now both threads seem to execute in parallel. Here is a program which shows the usage of the other overloaded method of sleep.

Program 6

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        int iHour = 0;
                        int iMin = 0;
                        int iSec = 1;

                        Console.WriteLine("Hello world " + i);
                        Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

The TimeSpan structure has four oveloaded constructors. The first take only one long paramater to a specified number of ticks, the second takes three int paramters for hours, mins and seconds respectively, the third take three int parameters for days, hours, mins and seconds and fourth overloaded constructor takes five parameter for days, hours, mins, seconds and milliseconds. The output of this program is the same except this program prints a new value after one second.

The Sleep method throws three type of exceptions; ArgumentException, when the time-out value is less than zero, ThreadInterruptedException when the thread is interrupted while sleeping and SecurityException when caller don’t have the appropriate permissions.

Let's see the program showing to handle the situation when the argument of Sleep() is negative.

Program 7

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        int iHour = 0;
                        int iMin = 0;
                        int iSec = -1;

                        try {
                                Console.WriteLine("Hello world " + i);
                                Thread.Sleep(new TimeSpan(iHour, iMin, iSec) );
                        }
                        catch (ArgumentException ae) {
                                Console.WriteLine(ae.Message );
                        }

                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Start();
                tid2.Start();
        }
}

Message is a property of the ArgumentException class giving the description of the exception. In this program it gives the error message

Parameter Name: Argument must be greater than 0 and less than 2^31 - 1milliseconds.

We can also assign a name to thread by using Name property of Thread.

Program 8

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                tid1.Start();
                tid2.Start();
        }
}

You should catch as many exceptions as possible to avoid run time crashes of the application. Let's try to handle all exceptions, which may be thrown from Sleep.

Program 9

using System;
using System.Threading;
using System.Security;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);

                        try {
                                Thread.Sleep(1);
        				}
                        catch (ArgumentException ae) {
                                Console.WriteLine(ae.ToString() );
                        }
                        catch (ThreadInterruptedException tie) {
                                Console.WriteLine(tie.ToString() );
                        }
                        catch (SecurityException se) {
                                Console.WriteLine(se.ToString() );
        				}
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
        		MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

        		tid1.Name = "Thread 1";
        		tid2.Name = "Thread 2";

                tid1.Start();
                tid2.Start();
        }
}

This program handles all the three exceptions. Take a look at the first line of the code. Here I use one more namespace: System.Security. The exception class SecurityException is defined in this namespace.

We have to get the reference of the thread in our thread function to get the name of the thread. We can get the current thread name by using CurrentThread. CurrentThread is a static property of the Thread class. This property throws only one exception: SecurityException when the caller doesn’t have appropriate Security Permissions.

Now the output of the program is

Before start thread
Thread 1=0
Thread 2=0
Thread 1=1
Thread 2=1
Thread 1=2
Thread 2=2
Thread 1=3
Thread 2=3
Thread 1=4
Thread 2=4
Thread 1=5
Thread 2=5
Thread 1=6
Thread 2=6
Thread 1=7
Thread 2=7
Thread 1=8
Thread 2=8
Thread 1=9
Thread 2=9

There are two methods to terminate thread. The first one is Stop() and second is Abort(). Don’t use Stop(), this function will not be in future releases of .NET; it will give you a warning. There are two overloaded methods of Abort. The first one executed without parameter and the second takes reference of Object as a parameter. This method throws ThreadAbortException that is not caught. Let’s see this program

Program 10

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);

                        try {
                                Thread.Sleep(1);
                        }
                        catch (ArgumentException ae) {
                                Console.WriteLine(ae.ToString() );
                        }
                        catch (ThreadInterruptedException tie) {
                                Console.WriteLine(tie.ToString() );
                        }
                        catch (SecurityException se) {
                                Console.WriteLine(se.ToString() );
                        }
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                tid1.Start();
                tid2.Start();

                try {
                        tid1.Abort();
                        tid2.Abort();
                }
                catch (ThreadAbortException tae) {
                        Console.WriteLine(tae.ToString() );
                }
                Console.WriteLine("End of Main");
        }
}

The output of this program is

Before start Thread
End of Main

This output clearly shows that no thread is executed. Once the thread is aborted then it cannot be started again. Take a look at the following program.

Program 11

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                tid1.Start();
                tid2.Start();

                tid1.Abort();
                tid2.Abort();

                Console.WriteLine("After Abort");

                tid1.Start();
                tid2.Start();

                Console.WriteLine("End of Main");
        }
}

This program throws an exception System.Threading.ThreadStateException. Now try to catch this exception and close the program gracefully rather than terminated abnormally.

Program 12

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                try {
                        tid1.Start();
                        tid2.Start();
                }
                catch (ThreadStateException te) {
                        Console.WriteLine(te.ToString() );
                }

                tid1.Abort();
                tid2.Abort();

                try {
                        tid1.Start();
                        tid2.Start();
                }
                catch (ThreadStateException te) {
                        Console.WriteLine(te.ToString() );
                }

                Console.WriteLine("End of Main");
        }
}

Here we catch ThreadStatException and we use the ToString method of this class. ToString method returns the fully qualified name of this exception, and possibly error message, name of the inner exception and stack trace.

We can also wait for terminating the thread by using the Join method. This method has three overloaded methods. First without parameter waits till the thread dies, second takes one int parameter and waits for the thread to die or for specified time to expire and third take a reference of an instance of the TimeSpan structure. See the following program.

Program 13

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                try {
                        tid1.Start();
                        tid2.Start();
                }
                catch (ThreadStateException te) {
                        Console.WriteLine(te.ToString() );
                }

                tid1.Join();
                tid2.Join(new TimeSpan(0, 0, 1) );

                Console.WriteLine("End of Main");
        }
}

Now the program waits for the first thread until it finishes and second thread for one second.

Threads can be executed in two ways: either in background or in foreground. A background thread is finished when the application is terminated, on the other hand foreground thread is not waiting for the termination of the application. We can set the execution of thread is by using IsBackground property. This program show the usage of this.

Program 14

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                tid1.IsBackground = true;
                tid2.IsBackground = true;

                try {
                        tid1.Start();
                        tid2.Start();
                }
                catch (ThreadStateException te) {
                        Console.WriteLine(te.ToString() );
                }

                Thread.Sleep(10);

                Console.WriteLine("End of Main");
        }
}

The output of this program is

Before start thread
Thread 1=0
Thread 2=0
Thread 1=1
Thread 2=1
End of Main

This output shows when your application terminates that both background threads are also terminated.

We can also assign the priority of our thread. We can assign the priority of a thread by using ThreadPriority priority of the Thread class. This program shows the usage of the priority property of the thread.

Program 15

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {
                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(thr.Name + "=" + i);
                        Thread.Sleep(1);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();
                MyThread thr2 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );

                tid1.Name = "Thread 1";
                tid2.Name = "Thread 2";

                tid1.Priority = ThreadPriority.Highest;
                tid2.Priority = ThreadPriority.Lowest;

                try {
                        tid1.Start();
                        tid2.Start();
                }
                catch (ThreadStateException te) {
                        Console.WriteLine(te.ToString() );
                }

	        	tid1.Join();
                tid2.Join();

                Console.WriteLine("End of Main");
        }
}

The property of thread 1 is Highest and thread 2 is Lowest. Other possible priority levels are AboveNormal, BelowNormal and Normal.

The GetDomain() method returns the name of executable file in which the thread is executing. This is a static method so we use it with the name of class.

Program 16

using System;
using System.Threading;

public class MyThread {

        public void Thread1() {

		Console.WriteLine(Thread.GetDomain() );

                for (int i = 0; i < 10; i++) {

                        Thread thr = Thread.CurrentThread;
                        Console.WriteLine(i);
                }
        }
}

public class MyClass {

        public static void Main() {
                Console.WriteLine("Before start thread");

                MyThread thr1 = new MyThread();

                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );

                tid1.Start();

        }
}
The output of this program is
Before start thread
Name: prog16.exe
No context policies.

0
1
2
3
4
5
6
7
8
9

This is just an introduction to multithreaded programming using C#.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zeeshan Amjad
Software Developer (Senior) Bechtel Corporation
United States United States
Member
Working as a C++ Developer at Bechtel Corporation.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionMultiple threading suggestion.memberpfsze23 Oct '12 - 9:17 
Two points I want to make on the multiple threading on real world programming.
<1>Two threads accessing two different memory location that will be OK because they are doing separate things. In that case, concurrency issue unlikely occurrs. so as to 'n' threading.
 
<2>Two threads accessing one block of memory location then one has to wait for the another. In that case creating more threads is just useless. eg. If you have n threads accessing one row on a table on SQL DB, then it will be case 2. All other threads have to wait until current job got done by previous one. That is the limitation of the concurrency programming.
 
Some people misunderstood the idea. They may think more threads means more quicker to get job done. That is totally wrong. It will be more slower oppositely if all threads are doing same job.
 
To be frankly, I really hate multiple threads. It is always messed up. LOL.....
Stefan Sze
Stefan Sze

GeneralMy vote of 5memberYNazm14 Feb '12 - 11:24 
شكرا لك
GeneralMy vote of 4memberbijaysalotry23 Oct '11 - 21:42 
Nice Article...
GeneralRe: Multithreading in C#groupAnkitaaguggi9 Sep '11 - 23:44 
check the following link.....
http://www.mindstick.com/Articles/13cd0056-e8ea-4637-b7c8-0146ea71fe4a/?Threading%20in%20C#%20with%20Example[^]
 
http://www.mindstick.com/Articles/99c775b1-ac52-42c4-a83c-3e44cc1698a3/?Join,%20Sleep%20and%20Abort%20methods%20in%20C#%20Threading[^]
Generalargument into function by refmemberigalep13223 Mar '10 - 3:36 
hey,
I've tried to implement your second example, but I've some difficulties over there...
 
i need to fill two dictionaries with data, so i send each dictionary from one class to MultiThreaded Class by ref...
here how it's looks like
 
public EnglishDataBase()
        {
            MultiThread mt = new MultiThread();
 
            englishCapLetter = new Dictionary<char, int>();
            englishSmallLetter = new Dictionary<char, int>();
 
            Thread t1 = new Thread(new ThreadStart(mt.fillCaps(ref englishCapLetter))));
            Thread t2 = new Thread(new ThreadStart(mt.fillUnCaps(ref englishSmallLetter)));
            
 
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
        }
 
public void fillCaps(ref Dictionary<char, int> englishCapLetter)
        {
            while (!sr1.EndOfStream)
            {
                string letter = sr1.ReadLine();
                englishCapLetter.Add(char.Parse(letter.ToUpper()), char.Parse(letter.ToUpper()));
            }
        }
 
        public void fillUnCaps(ref Dictionary<char, int> englishSmallLetter)
        {
            while (!sr2.EndOfStream)
            {
                string letter = sr2.ReadLine();
                englishSmallLetter.Add(char.Parse(letter.ToLower()), char.Parse(letter.ToLower()));
            }
        }
 
but then i get a "Method name expected" compilation error...
what's wrong with implementation i did ?
 
tanks in advance
GeneralRe: argument into function by refmemberigalep13223 Mar '10 - 4:02 
ok,
i think i've found a solution...
i solved it like this :
 
Thread t1 = new Thread(new ParameterizedThreadStart(mt.fillCaps));
            t1.Start(englishCapLetter);
 
it seems like it work fine
 
thanks any way
GeneralThreadings becomes familiar by this introductionmemberskokeh31 May '08 - 21:38 
I thank that threading very hard to programming it, but now it became easy to use and to understand it by this articles.
congratulation to programmers who don't know how to use threading, now you can understand it by this article.
 
Software Eng.
Mhd Samer
 
Mhd Sam Kouka

QuestionReturning value of method called by threadmembershahid hameed19 May '08 - 23:09 
public dataset fun (){return new DataSet();}
public void mainfun (){DataSet ds = fun();}
 
How to call method [fun] in [mainfun] method using Thread so that method [fun]returned dataset will be assigned to dataset [ds] in [mainfun]
AnswerRe: Returning value of method called by threadmemberSrinivasgorantla14 May '09 - 0:08 
hi please send me the complete code about this article
GeneralReturn Values from Thread class to Main classmemberMubarak_Shahul3 Sep '07 - 21:17 

Hi Amjad,

I have Simple or Funny Doubt regarding Multiple Thread, Is There way to return values from thread class to main class.Please reply as soon as Possible.
 
Regards,
Mubarak

GeneralTwo Thread communcationmemberrobin_cbd24 Mar '07 - 4:35 
Dear:
 
I want to ask how to implement one thread control the other thread. I means two threads communication?
 
Thanks.
 
chen
QuestionMutex?memberriscy12 Mar '07 - 3:37 
I hear .Start() is no longer support for future .NET library and is being phase out.
Can someone offer alternative tutorial covering mutex, I think this is a missing link.
Thanks

 
>It's not so much what you have to learn if you accept weird theories, it's what you have to unlearn. (Isaac Asimov)
>Life's journey is not to arrive at the grave safely in a well preserved body,but rather to skid in sideways, totally worn out, shouting "...holy sh*t...what a ride! [Riscy]

Generalproblem in Multi threadingmembernmehta00526 Feb '07 - 3:42 
Problem:
 
1. I have save detail method on server which take long time to dave data in database because it is inserting 10000+ records each time and my frond end application hangs for 2-3 minutes.
 
Through safe threading i want to achvive this task and I also want to know how many threads are running on server because this methods might be call from other user.
Can i also get % of complition of task by each thread?
Generalhelp mememberali.soltani20 Oct '06 - 23:56 
i need to help.i dont know how create thread in WindowsApplication?can you help me?Confused | :confused:
Questionhelp mememberali.soltani20 Oct '06 - 23:51 
i need help to start write program by thread?where must write thread code?Confused | :confused:
QuestionMultithreaded Windows ServicememberAdnan Siddiqi26 Sep '06 - 21:18 
is there any tip to implement multithreading in a windows service? I didnt find any related article/tutorial yet.
QuestionIs it possible to use a Thread to handle an eventmembershero4ever21 Aug '06 - 0:11 
hello
I have a KeyDown event, and wants a Thread to handle this event?
can this be done? and if yes? then how??
thnx
GeneralWonderful introduction!memberwooohoh5 Sep '05 - 19:54 
Thanks very much for such a wonderful article.
 
It helped me a lot Big Grin | :-D
GeneralGood Introduction to ThreadssussChunkie19 Jul '05 - 23:04 
Nice article, picked up a couple of pointers from reading it.
 
Keep up the good work, well done!
Ambrose Laugh | :laugh:
 

GeneralThreadmemberpersée9 Jun '05 - 20:49 
Hi,
 
It is possible to use a thread inside an object other than the main thread ?
 
txs
Generalprogram3memberToximus9 Feb '05 - 23:19 
I wish to know how compiler will interpet following situation.
Instead of creating two object MyThread, I create only one.

// part of program3
...
public static void Main() {
Console.WriteLine("Before start thread");
 
MyThread thr1 = new MyThread();
MyThread thr2 = new MyThread();
 
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
 
tid1.Start();
tid2.Start();
}
...

Is there any diffrence in executing this code instead?

// modified part of program3
...
public static void Main() {
Console.WriteLine("Before start thread");
 
MyThread thr1 = new MyThread();
 
Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
Thread tid2 = new Thread(new ThreadStart(thr1.Thread1) );
 
tid1.Start();
tid2.Start();
}
...

GeneralRe: program3memberArt of War28 Aug '07 - 17:21 
yeah, of course. it is different.
there may be some conflit in second codes.
the two threads will operate the same variable. it will effects the result
Generalnice articlesussdavidweimin29 Dec '04 - 20:39 
good job!
GeneralA useful articlememberkocamane17 Aug '04 - 5:25 
This article helped me while I debugging my application.
 
Note : You have to set isBackground property of threads you used in an application true to prevent application running as background process when you close the application.
 

Thanks a lot.
Generalboring - to say the leastmemberchrisy12 Nov '03 - 1:25 
boring - to say the least

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 11 Oct 2001
Article Copyright 2001 by Zeeshan Amjad
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid