C# - How to easily create and start a new thread

It is quite easy to start a new thread in C#. All you need is to instantiate a new object of the type Thread and call the Start() method on it, like shown below:

new Thread(() => {
   //Your code here
}).Start();

The comment //Your code here describes where to put your code that should be run in the thread. Before you start to worry: no you do not need to dispose your thread when you are done with it, it will clean itself up. Alternatively if you need to set some fields on the thread such as IsBackground, you can assign your thread instance to a variable and do the necessary changes before you start it:

var thread = new Thread(() =>
{
    //Your code here
});
thread.IsBackground = true;
thread.Start();

If you just want to run something asynchronously, I would suggest that you use tasks as these use the thread pool which can save you resources. It is even easier to start a new Task as I demonstrate in this post.

I hope this helped you to create and run code within another thread, please let me know what you think in the comments down below!