c# - Start multiple threads at same time and UnitTest it -


I was wondering how can I test that the code I was running actually started at the same time Gone ...

Here is a piece of code:

  var thread = new list & lt; Thread & gt; {New Thread ((=)} Call 1 Stuff (), New Thread ((=); Call 2 DundFunk ())} Thread Forac (T = & gt; T. Start ()); Threads Foreike (T => T. join ());  

How can I test it and verify that the thread is executed at the same time?
If my code is wrong, then I would know that what is the right code to perform such a task?

You can not start threads at the same time and the execution order is not determined. But you can use synchronization build to create synchronization. For example, use manual reset antenatal so that your threads do not wait until it is set.

You can use the thread number and time marker for unit testing. Simply submit them to the list and then compare the start time with the delta.

  Fixed zero main (string [] arg) {var ids = new [] {"1", "2", "3"}; Var manualResetEventSlim = New manual racetensement (); Foreach (different IDs in id) {var thread = new thread (obj) => {Console.WriteLine (string.Format ("threaded {0} strated", as obj string); manualResetEventSlim.Wait ); Console .itline ("Thread {0} work in progress", as OBG string);}; Thread.Start (id); } Console. Readkey (); ManualResetEventSlim.Set (); Console.ReadKey (); }  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -