Today I will show you more about C# and the subject of this tutorial is threads.
First, you need to start a project into your folder. Let’s see my example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | C:\BackUP\Proiecte\CSharpNET>dotnet new console -o threads001 The template "Console Application" was created successfully. C:\BackUP\Proiecte\CSharpNET>dotnet restore threads001 Restoring packages for C:\BackUP\Proiecte\CSharpNET\threads001\threads001.csproj... Generating MSBuild file C:\BackUP\Proiecte\CSharpNET\threads001\obj\threads001.csproj.nuget.g.props. Generating MSBuild file C:\BackUP\Proiecte\CSharpNET\threads001\obj\threads001.csproj.nuget.g.targets. Restore completed in 10.55 sec for C:\BackUP\Proiecte\CSharpNET\threads001\threads001.csproj. C:\BackUP\Proiecte\CSharpNET>cd threads001 C:\BackUP\Proiecte\CSharpNET\threads001>dotnet run Hello World! |
You can use notepad or any editor to edit the Program.cs:
1 | C:\BackUP\Proiecte\CSharpNET\threads001>notepad Program.cs |
About the thread issue then you need to know is a very complex subject.
My example will show you how the thread works.
First, you need to start the thread and then deal with the thread.
Two methods are used: Start and Join.
Start begins executing at the first line of the method and does not block the calling thread.
Join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until the thread whose Join method is called has completed.
I used three threads and these threads just start count first 5 sec, next 8 sec and the last is set to 10 sec.
Let’s see the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace threads001 { public class Program { static void StartMultipleThread() { //take time DateTime startTime = DateTime.Now; //parameterized thread nr. 1 Thread Thread_nr_1 = new Thread(() => { int numberOfSeconds = 0; while (numberOfSeconds < 4) { Thread.Sleep(1000); numberOfSeconds++; } Console.WriteLine("Thread_nr_1 = 5 seconds"); }); //parameterized thread nr. 2 Thread Thread_nr_2 = new Thread(() => { int numberOfSeconds = 0; while (numberOfSeconds < 7) { Thread.Sleep(1000); numberOfSeconds++; } Console.WriteLine("Thread_nr_2 = 8 seconds"); }); //parameterized thread nr. 3 Thread Thread_nr_3 = new Thread(p => { int numberOfSeconds = 0; while (numberOfSeconds < Convert.ToInt32(p)) { Thread.Sleep(1000); numberOfSeconds++; } Console.WriteLine("Thread_nr_3 = {0} seconds", numberOfSeconds); }); //Start the threads nr. 1 , 2 Thread_nr_1.Start(); Thread_nr_2.Start(); //Start thread nr. 3 with 10 sec Thread_nr_3.Start(10); //wait for Thread_nr_1 to fimish Thread_nr_1.Join(); //wait for Thread_nr_2 to finish Thread_nr_2.Join(); //wait for Thread_nr_3 to finish Thread_nr_3.Join(); Console.WriteLine("Threads end in {0} secods", (DateTime.Now - startTime).TotalSeconds); } static void Main(string[] args) { StartMultipleThread(); Console.WriteLine("Hello World!"); } } } |
Each thread will write to the output and then will see the time used.
See the output of dotnet run command:
1 2 3 4 5 6 | C:\BackUP\Proiecte\CSharpNET\threads001>dotnet run Thread_nr_1 = 5 seconds Thread_nr_2 = 8 seconds Thread_nr_3 = 10 seconds Threads end in 10.0089403 secods Hello World! |