Showing posts with label Multi-threading in C#. Show all posts
Showing posts with label Multi-threading in C#. Show all posts

Monday, 22 December 2014

Multi-threading in C#,Multithreaded Programming Using C#


Hai friend today I am going to say about Multithreaded Programming Using C#
In .net Multithreaded stands for executing the code parallel when we take two methods  and execute the code is executed sequential manner the code first method and then come to second method .if u want to code execute parallely we go for Multithreaded concept 

Program:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread obj1 = new Thread(data1);
            Thread obj2 = new Thread(data2);
            obj1.Start();
            obj2.Start();
           
        }

        public static void data1()
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine("this is my data" + i.ToString());
            }
        }

        public static void data2()
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine("this is my newdata" + i.ToString());
            }
            Console.ReadLine();
        }
    }
}

  Output:



  




Threads are of two type Fore Ground thread and background thread
When coming to Fore Ground thread even your main application quit then also they will thread complete their task
When come to back ground threads which will quit whenever main application quit