Create a thread in simple four steps


This code snippet shows to create and start a thread

To create and start a new thread, follow these steps:
Step 1.Create a method that takes no arguments and does not return any data (for
example, use the void return type for C#). This method should look something
this:


// C#
static void SimpleWork()
{
Console.WriteLine("Thread: {0}", Thread.CurrentThread.ManagedThreadId);
}


step 2;- Create a new ThreadStart delegate, and specify the method created in step 1.
step 3:- Create a new Thread object, specifying the ThreadStart object created in step 2.
step 4. Call Thread.Start to begin execution of the new thread. This is completion of code


ThreadStart operation = new ThreadStart(SimpleWork);
// Creates, but does not start, a new thread
Thread theThread = new Thread(operation);
// Starts the work on a new thread
theThread.Start();


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: