Advertisement
wingman007

ThreadingC#

Nov 4th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Threding
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Paralel code execution
  11.             // https://www.youtube.com/watch?v=Fzckqczmgd0
  12.  
  13.             // synhronous/single threated execution
  14.             //Function1();
  15.             //Function2();
  16.  
  17.             // 1. Foreground threads
  18.             // Paralel/multithreated asynchronous execution
  19.             // forground threads
  20.             // Thread obj1 = new Thread(Function1);
  21.             //  Thread obj2 = new Thread(Function2);
  22.             // start the paralel threads
  23.             // forground threads - even if the main finishes the threads will continue
  24.             // obj1.Start();
  25.             // obj2.Start();
  26.             // here the main app quits but the foreground threads continue
  27.  
  28.             // Background threads will quit if the main app quits
  29.  
  30.             // 2. Background thread
  31.             // By default the thread is forground
  32.             Thread obj1 = new Thread(Function1);
  33.             // !!! To make it background
  34.             obj1.IsBackground = true; // make it beackground
  35.             obj1.Start();
  36.             Console.WriteLine("The main application has exited");
  37.         }
  38.  
  39.         // For forground thread
  40.         //static void Function1()
  41.         //{
  42.         //    for (int i = 0; i < 10; i++)
  43.         //    {
  44.         //        Console.WriteLine("Function 1 executed " + i.ToString());
  45.         //        // to see the thread execution clearly we wait for 4 s
  46.         //        Thread.Sleep(4000);
  47.         //    }
  48.         //}
  49.  
  50.             // For background thread
  51.         static void Function1()
  52.         {
  53.  
  54.             Console.WriteLine("Function 1 is entered ...");
  55.             Console.ReadLine(); // wait here
  56.             Console.WriteLine("Function 1 is exited ...");
  57.         }
  58.  
  59.         static void Function2()
  60.         {
  61.             for (int i = 0; i < 10; i++)
  62.             {
  63.                 Console.WriteLine("Function 2 executed " + i.ToString());
  64.                 // to see the thread execution clearly we wait for 4 s
  65.                 Thread.Sleep(4000);
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. /*
  72.  Function 1 executed 0
  73. Function 1 executed 1
  74. Function 1 executed 2
  75. Function 1 executed 3
  76. Function 1 executed 4
  77. Function 1 executed 5
  78. Function 1 executed 6
  79. Function 1 executed 7
  80. Function 1 executed 8
  81. Function 1 executed 9
  82. Function 2 executed 0
  83. Function 2 executed 1
  84. Function 2 executed 2
  85. Function 2 executed 3
  86. Function 2 executed 4
  87. Function 2 executed 5
  88. Function 2 executed 6
  89. Function 2 executed 7
  90. Function 2 executed 8
  91. Function 2 executed 9
  92.  
  93. Paralel execution
  94. Function 2 executed 0
  95. Function 1 executed 0
  96. Function 1 executed 1
  97. Function 2 executed 1
  98. Function 2 executed 2
  99. Function 1 executed 2
  100. Function 2 executed 3
  101. Function 1 executed 3
  102. Function 2 executed 4
  103. Function 1 executed 4
  104. Function 2 executed 5
  105. Function 1 executed 5
  106. Function 2 executed 6
  107. Function 1 executed 6
  108. Function 2 executed 7
  109. Function 1 executed 7
  110. Function 2 executed 8
  111. Function 1 executed 8
  112. Function 1 executed 9
  113. Function 2 executed 9
  114.  
  115.     Function 1 is entered ...
  116. The main application has exited
  117. eww
  118. Function 1 is exited ...
  119.  
  120.     background thread
  121.     The main application has exited
  122. Function 1 is entered ...
  123.      */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement