ThreadStart Delegate


This represents the method that should be executed on a thread
This delegate is passed to the Thread constructor
This method is not called until the Thread.Start method is called


System.Threading.ThreadStart myThreadStart; 
myThreadStart = new System.Threading.ThreadStart(myMethod);

System.Threading.Thread myThread;
myThread = new System.Threading.Thread( myThreadStart );
myThread.Start();

for (int i=0; i<100; i++)
{
   Console.Write("main thread");
}

void myMethod()
{
   for (int i=0; i<100; i++)
   {
      Console.Write("second thread");
   }
}


ParameterizedThreadStart Delegate

This has an argument of data type object.
This delegate only allows you to pass in a single parameter and does so in an unsafe way because it passes it as an object.


System.Threading.Thread myThread; 
myThread = new System.Threading.Thread (new Systm.Threading.ParameterizedThreadStart(myMethod) );

public static void myMethod(object myParam)
{}
myThread.Start(myObjectParameter);


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext