Should I have to really use ThreadStart to start the thread?

No not at all !!! See the below example. Here the compiler automatically generates the ThreadStart delegate for you. Thanks to C# short cuts.



namespace My_Samples
{
using System.Threading;
using System;
using System.Diagnostics;
using System.Reflection;

class Program
{
static void Main(string[] args)
{
//Without Thread Start. Implicitly Compiler declares ThreadStart for you.
Thread thread = new Thread(MyMethod);
thread.Start();
Console.WriteLine(GetMethodName());
Console.ReadLine();
}

static void MyMethod()
{
Console.WriteLine(GetMethodName());
}

private static string GetMethodName()
{
StackTrace callStack = new StackTrace();
StackFrame frame = callStack.GetFrame(1);
MethodBase method = frame.GetMethod();
return ">>>" + method.DeclaringType.FullName + "->" + method.ToString();
}
}
}

No comments:

Post a Comment