Multi Threading Basic C# with Example



Multi Threading Basic C# with Example

 using System;
using System.Threading;

namespace CSharpMultiThreading
{
    public delegate int Addition(int x, int y);

    class Program
    {
      static void Main(string[] args)
      {
      // Print out the ID of the executing thread.
      Console.WriteLine("Main() invoked on thread {0}.",Thread.CurrentThread.ManagedThreadId);

      // Invoke Add() in a synchronous manner.
      Addition b = new Addition(Add);

      // Could also write b.Invoke(10, 10);
      int answer = b(10, 10);

      // These lines will not execute until the Add() method has completed.
      Console.WriteLine("Doing more work in Main()!");
      Console.WriteLine("10 + 10 is {0}.", answer);
      }

      static int Add(int x, int y)
      {
        // Print out the ID of the executing thread.
        Console.WriteLine("Add() invoked on thread {0}.",Thread.CurrentThread.ManagedThreadId);

        // Pause to simulate a lengthy operation.
        Thread.Sleep(5000);
        return x + y;
      }
    }
}
 

0 Comment's

Comment Form

Submit Comment