Sunday, 29 December 2019

C# Multithreading


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

namespace Multithreading
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create child threads
            Thread t1 = new Thread(new ThreadStart(Task1));
            Thread t2 = new Thread(new ThreadStart(Task2));

            t1.Start();
            t2.Start();

            Console.ReadLine();
        }

        static void Task1()
        {
            for (int i = 1; i <= 4; i++)
            {
                Console.WriteLine("First Thread - value: {0}", i);

                Thread.Sleep(1000);
            }

            Console.WriteLine("First method completed");
        }

        static void Task2()
        {
            for (int i = 1; i <= 4; i++)
            {
                Console.WriteLine("Second Thread - value: {0}", i);
            }

            Console.WriteLine("Second method completed");
        }
    }
}


No comments:

Post a Comment