Multi-threading in C#

A simple mult-threading in c# .

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class MyThread
    {
        public static void Main(String[] args)
        {
            Console.WriteLine("Threading test : ");


            Task1 t_one = new Task1();
            t_one.num = 5;

            //Thread one
            Thread thread1 = new Thread(t_one.Print);
            thread1.Name = "Thread 1";
            thread1.Start();

            //Thread two
            Thread thread2 = new Thread(t_one.Print);
            thread2.Name = "Thread 2";
            thread2.Start();

            thread1.Join();
            thread2.Join();

            Console.WriteLine("All done");

            Console.ReadKey();
        }
    }

    class Task1
    {
        public int num;

        public void Print()
        {
           
            for(int i=0;i< num;i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + " : " + i);
                Thread.Sleep(1000);
            }
            Console.WriteLine(Thread.CurrentThread.Name +" done");
        }
    }
}

About Max
My Interests: ------------------ Programming

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: