Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Multithreaded Programming Using C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (164 votes)
10 Oct 2001CPOL6 min read 856.3K   323  
A simple tutorial on Multithreaded Programming using C#
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();
        }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions