Create New Thread [C#]
This example shows 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.
[C#]using System.Threading; Thread thread = new Thread(new ThreadStart(WorkThreadFunction)); thread.Start();
The WorkThreadFunction could be defined as follows.
[C#]public void WorkThreadFunction() { try { // do any background work } catch (Exception ex) { // log errors } }
See also
- [C#] Create an Asynchronous Method – how to invoke an asynchronous method
- [C#] Asynchronous Method Progress Reporting – how to report progress to client code
- [C#] Cancel an Asynchronous Method – implement cancellation support
- [C#] Catching Unhandled Exceptions – how to catch unhandled thread exceptions
- [C#] Open File With Associated Application – how to launch the default application
- Thread – MSDN – a class representing a thread in .NET Framework
- ThreadStart – MSDN – a delegate to a method that is run by a thread
- Thread.Start – MSDN – a method that starts the thread