It has to be said that the mechanism of synchronous threading in C # is really much.
Various. There are synchronization mechanisms that can be used in almost every scenario.
What I said today is more interesting.
The mechanism of waiting is very simple, waiting simply. I'll wait for the way I use it. The things you wait for or the content you set yourself.
Quite well understood.
This thing in the book has a bigger role in iteration.
classProgram {StaticBarrier Barrier =NewBarrier (1, B=>console.writeline ($"End of Phase{b.currentphasenumber+1}")); Static voidPlaymusic (stringNamestringMessageintseconds) { for(inti =2; I <3; i++) {Console.WriteLine ("----------------------------------------------------"); Thread.Sleep (timespan.frommilliseconds (seconds)); Console.WriteLine ($"{Name} finishes to {message}"); Barrier. SignalAndWait (); } } Static voidMain (string[] args) { varT1 =NewThread (() = Playmusic ("M1","Good Music",5)); vart2 =NewThread (() = Playmusic ("m2","Bad music",5)); T1. Start (); T2. Start (); Console.readkey (); } }
The special place is when barrier the arguments to the constructor at the time of instantiation,
The first parameter is a few times or is understood to have several different places or threads using barrier
The second parameter is nullable and is a callback function. When to use it is called when barrier uses the SignalAndWait method. Wait for the content is also here
The rest is to see the SignalAndWait method.
"C # Multithreaded Programming combat" 2.8 Barrier