This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ShareDataInstanceVariable | |
| { | |
| string instanceString = "*"; | |
| public void Print(string threadMethodParam) | |
| { | |
| for (int i = 0; i < 3; i++) | |
| { | |
| Console.WriteLine(string.Format("ThreadID: {0} {1} {2}", | |
| Thread.CurrentThread.ManagedThreadId, threadMethodParam, instanceString)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static string ClassString = "*"; | |
| static void Main(string[] args) | |
| { | |
| Program program = new Program(); | |
| //Another thread | |
| Thread newThread = new Thread(() => program.Print("+")); | |
| newThread.Start(); | |
| //Main thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static List<int> sourceList = new List<int>() { 1,2,3,4,5}; | |
| static List<int> destinationList = new List<int>(); | |
| static void Main(string[] args) | |
| { | |
| //Create new threads | |
| Thread newThread1 = new Thread(Move); | |
| Thread newThread2 = new Thread(Move); | |
| //Start thread |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Method with no parameter - ThreadStart Delegate | |
| Thread t = new Thread (new ThreadStart (TestMethod)); | |
| t.Start(); | |
| void TestMethod() {} | |
| //Method with a parameter - ParameterizedThreadStart Delegate | |
| Thread t = new Thread (new ThreadStart (TestMethod)); | |
| t.Start(5); | |
| t.Start("test"); | |
| void TestMethod(Object o) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Bad | |
| public static void Main() | |
| { | |
| try | |
| { | |
| new Thread (Go).Start(); | |
| } | |
| catch (Exception ex) | |
| { | |
| // We'll never get here! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Nested locking is useful when one method calls another within a lock: | |
| static readonly object _locker = new object(); | |
| static void Main() | |
| { | |
| lock (_locker) | |
| { | |
| AnotherMethod(); | |
| // We still have the lock - because locks are reentrant. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A deadlock happens when two threads each wait for a resource held by the other, so neither can proceed. | |
| object locker1 = new object(); | |
| object locker2 = new object(); | |
| new Thread (() => { | |
| lock (locker1) | |
| { | |
| Thread.Sleep (1000); | |
| lock (locker2); // Deadlock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //WinForms Control.Invoke | |
| public Object Invoke( | |
| Delegate method | |
| ) | |
| //WPF Dispatcher.Invoke | |
| public Object Invoke( | |
| DispatcherPriority priority, | |
| TimeSpan timeout, | |
| Delegate method, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Modifying IEnumerable while another thread enumerates it, will throw exception | |
| //Every thread entry method should handle exception, if not will kill entire app. | |
| static List<int> sourceList = new List<int>() { 1, 2, 3, 4, 5 }; | |
| static void Main(string[] args) | |
| { | |
| //One thread enumerates thread while another thread modifies the collection | |
| new Thread(Iterate).Start(); | |
| new Thread(Add).Start(); | |
| Console.ReadKey(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Concurrent reading is thread safe | |
| static List<int> sourceList = new List<int>() { 1, 2, 3, 4, 5 }; | |
| static void Main(string[] args) | |
| { | |
| new Thread(Iterate).Start(); | |
| new Thread(Iterate).Start(); | |
| Console.ReadKey(); | |
| } |