In this part of my C# tutorial I’ll cover threads in depth. With threads you can execute multiple pieces of code that share resources and data without corrupting it. I cover a simple thread example, sleep, a more advanced with lock, Priority, and how to pass data to a thread.
For best results take notes on the cheat sheet provided above as you watch and leave any questions you have.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Threading; namespace CSharpTutA.cs { // With threads you can execute multiple // pieces of code that share resources // and data without corrupting it // You can't guarantee when a thread // executes. You also must lock resources // until a thread is done with them // or you could corrupt them class Program { // ----- Simple Thread Example ----- /* static void Main(string[] args) { // Create a thread and start it Thread t = new Thread(Print1); t.Start(); // This code will run randomly for (int i = 0; i < 1000; i++) Console.Write(0); Console.ReadLine(); } static void Print1() { for (int i = 0; i < 1000; i++) Console.Write(1); } // ----- Sleep Example ----- // With sleep() the thread is suspended // for the designated amount of time static void Main(string[] args) { int num = 1; for (int i = 0; i < 10; i++) { Console.WriteLine(num); // Pause for 1 second Thread.Sleep(1000); num++; } Console.WriteLine("Thread Ends"); Console.ReadLine(); } // ----- Lock Example ----- // lock keeps other threads from entering // a statement block until another thread // leaves static void Main(string[] args) { BankAcct acct = new BankAcct(10); Thread[] threads = new Thread[15]; // CurrentThread gets you the current // executing thread Thread.CurrentThread.Name = "main"; // Create 15 threads that will call for // IssueWithdraw to execute for (int i = 0; i < 15; i++) { // You can only point at methods // without arguments and that return // nothing Thread t = new Thread(new ThreadStart(acct.IssueWithdraw)); t.Name = i.ToString(); threads[i] = t; } // Have threads try to execute for (int i = 0; i < 15; i++) { // Check if thread has started Console.WriteLine("Thread {0} Alive : {1}", threads[i].Name, threads[i].IsAlive); // Start thread threads[i].Start(); // Check if thread has started Console.WriteLine("Thread {0} Alive : {1}", threads[i].Name, threads[i].IsAlive); } // Get thread priority (Normal Default) // Also Lowest, BelowNormal, AboveNormal // and Highest // Changin priority doesn't guarantee // the highest precedence though // It is best to not mess with this Console.WriteLine("Current Priority : {0}", Thread.CurrentThread.Priority); Console.WriteLine("Thread {0} Ending", Thread.CurrentThread.Name); Console.ReadLine(); } } } class BankAcct { private Object acctLock = new Object(); double Balance { get; set; } string Name { get; set; } public BankAcct(double bal) { Balance = bal; } public double Withdraw(double amt) { if((Balance - amt) < 0) { Console.WriteLine($"Sorry ${Balance} in Account"); return Balance; } lock (acctLock) { if(Balance >= amt) { Console.WriteLine("Removed {0} and {1} left in Account", amt, (Balance - amt)); Balance -= amt; } return Balance; } } // You can only point at methods // without arguments and that return // nothing public void IssueWithdraw() { Withdraw(1); } } */ // ----- Passing Data to Threads ----- // You can pass arguments to a thread // using a lambda expression static void Main(string[] args) { Thread t = new Thread(() => CountTo(10)); t.Start(); // You can use multiline lambdas new Thread(() => { CountTo(5); CountTo(6); }).Start(); Console.ReadLine(); } static void CountTo(int maxNum) { for(int i = 0; i <= maxNum; i++) { Console.WriteLine(i); } } } } |
Leave a Reply