Jul 1, 2012

How to Create Thread in .Net?

1 comment
This example explains how to create a new thread in .NET Framework.

First, create a new ThreadStart delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. 

Finally, call the Thread.Start method to run your method (in this case WorkThreadFunction) on background.

Code:
using System.Threading;

Thread thread = new Thread(new ThreadStart(ThreadFunction));
thread.Start();

The ThreadFunction to be defined as follows.

 Code:
public void ThreadFunction()
{
  try
  {
    // perform any background work
  }
  catch (Exception ex)
  {
    // log errors
  }
}

1 comment :