+ Reply to Thread
Results 1 to 2 of 2

Thread: Multi Thread example

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    35

    Multi Thread example

    Hi , i'd like to know more about multi-threading , can you give me a short tutorial ?
    Last edited by boss91; 11-11-2012 at 10:58 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    35
    i am going to give an example of how to do a multi threading (tasking)
    we will use 3 methods to print 3 different values (x,y,z)
    So the first thing you should consider is importing the
    Code:
    using System.Threading;
    And then add the following code :
    Code:
    class THREAD_TEST
    {
        string x = "x";
        string y = "y";
        string z = "z";
        static void Main(string[] args)
        {
    
            THREAD_TEST p = new THREAD_TEST();
    
    
            Thread t1 = new Thread(new ThreadStart(p.m1));
            Thread t2 = new Thread(new ThreadStart(p.m2));
            Thread t3 = new Thread(new ThreadStart(p.m3));
    
    
            t1.Start();
            t2.Start();
            t3.Start();
    
        }
        private void m1()
        {
            for (int i = 0; i < 20; i++)
            {
                Console.Write(x);
            }
        }
        private void m2()
        {
            for (int i = 0; i < 20; i++)
            {
                Console.Write(y);
            }
        }
        private void m3()
        {
            for (int i = 0; i < 20; i++)
            {
                Console.Write(z);
            }
        }
    }
    Output should be like that :
    Code:
    xyyyyyyyyxxyyyyyyyyyyxxxxxxxxxxzzxxxyyxxxxxxzzzzzzzzzzzzzzzzzz

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts