Method I found easy.
Method 1:
System.Threading.ThreadPool.QueueUserWorkItem(c => MethodCall())
Ex:
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start at :" + DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff"));
System.Threading.ThreadPool.QueueUserWorkItem(c => PrintTime());
System.Threading.ThreadPool.QueueUserWorkItem(c => PrintTime());
System.Threading.ThreadPool.QueueUserWorkItem(c => PrintTime());
System.Threading.ThreadPool.QueueUserWorkItem(c => PrintTime());
System.Threading.Thread.Sleep(10000);
Console.WriteLine("Main Thread End at :" + DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff"));
Console.ReadKey();
}
private static void PrintTime()
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff"));
}
Method 2:
System.Threading.Thread obj = new System.Threading.Thread(() => MethodCall(Arg)));
obj.IsBackground = true;
obj.Start();
Ex:
static void Main(string[] args)
{
Console.WriteLine("Main Thread Start at :" + DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff"));
System.Threading.Thread t1 = new System.Threading.Thread(() => PrintTime(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff")));
System.Threading.Thread t2 = new System.Threading.Thread(() => PrintTime(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff")));
System.Threading.Thread t3 = new System.Threading.Thread(() => PrintTime(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff")));
System.Threading.Thread t4 = new System.Threading.Thread(() => PrintTime(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff")));
System.Threading.Thread t5 = new System.Threading.Thread(() => PrintTime(DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff")));
t1.IsBackground = true;
t2.IsBackground = true;
t3.IsBackground = true;
t4.IsBackground = true;
t5.IsBackground = true;
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
System.Threading.Thread.Sleep(10000);
Console.WriteLine("Main Thread End at :" + DateTime.Now.ToString("YYYY/MM/dd HH:mm:ss.ffffff"));
Console.ReadKey();
}
private static void PrintTime(String CurrentTime)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine(CurrentTime);
}
No comments:
Post a Comment