Updated 7/17/2018
if you've never created a multi-threaded app, this is basic introduction of threading in .NET using C#.
The Thread class is defined in the System.Threading namespace must be imported before you can use any threading related types.
The Thread constructor takes a ThreadStart delegate as a parameter and creates a new thread. The parameter of the ThreadStart is the method that is executed by the new thread. Once a thread it created, it needs to call the Start method to actually start the thread.
The following code snippet creates a new thread, workerThread that will execute code in the Print method.
The Print method is listed below that can be used to execute code to do some background or foreground work.
Let’s try it.
Open Visual Studio. Create a new .NET Core console project. Delete all code and copy and paste (or type) the code in Listing 1.
Listing 1.
The code of Listing 1, the main thread prints 1 to 10 after every 0.2 seconds. The secondary thread prints from 11 to 20 after every 1.0 second. We’re using the delay for the demo purpose, so you can see live how two threads execute code parallelly.