Auto Save Event C# with Example



Auto Save Event C# with Example

 using System;
using System.Threading;
using System.Windows.Forms;

namespace CSharpMultiThreading
{
    class AddParams
    {
        public int a, b;

        public AddParams(int numb1, int numb2)
        {
            a = numb1;
            b = numb2;
        }
    }

    class UsingParameterizedThreadStartDelegate
    {
        static void Add(object data)
        {
            if (data is AddParams)
            {
                Console.WriteLine("ID of thread in Add(): {0}",
                Thread.CurrentThread.ManagedThreadId);
                AddParams ap = (AddParams)data;
                Console.WriteLine("{0} + {1} is {2}", ap.a, ap.b, ap.a + ap.b);
            }
			
			// Tell other thread we are done.
			waitHandle.Set();
        }

		private static AutoResetEvent waitHandle = new AutoResetEvent(false);
        static void Main(string[] args)
        {
            Console.WriteLine("ID of thread in Main(): {0}", Thread.CurrentThread.ManagedThreadId);

            // Make an AddParams object to pass to the secondary thread.
            AddParams ap = new AddParams(10, 10);
            ParameterizedThreadStart secondaryThreadCode = new ParameterizedThreadStart(Add);
            Thread t = new Thread(secondaryThreadCode);
            t.Start(ap);

			// Wait here until you are notified!
			waitHandle.WaitOne();
			Console.WriteLine("Other thread is done!");
        }
    }
}
 

0 Comment's

Comment Form