How do I run a simple bit of code in a new thread?


Question

I have a bit of code that I need to run in a different thread than the GUI as it currently causes the form to freeze whilst the code runs (10 seconds or so).

Assume I have never created a new thread before; what's a simple/basic example of how to do this in C# and using .NET Framework 2.0 or later?

1
319
4/4/2013 2:41:08 PM

Good place to start reading is Joe Albahari.

If you want to create your own thread, this is as simple as it gets:

using System.Threading;
new Thread(() => 
{
    Thread.CurrentThread.IsBackground = true; 
    /* run your code here */ 
    Console.WriteLine("Hello, world"); 
}).Start();
312
8/11/2017 1:40:52 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon