1

I need to test my vigorously tested code works when called by many threads

So I am calling the code in a loop in a TestMethod using TreadPool.QueueUserWorkItem

however there does not seem to be anyway of holding the current thread while all the threads kicked off are still running.

i.e. pretend code....

using Microsoft.VisualStudio.TestTools.UnitTesting

10 create a server

20 for( a number of iterations ) start a thread running a function

30 while all the threads are still doing stuff hang on to the server

40 now dispose of the server

Without the wait my unit test is disposing of the server before the job is done.

Any insights gratefully received...?

Anthony Johnston
  • 9,405
  • 4
  • 46
  • 57

3 Answers3

0

Is your goal for the main thread to wait for all the other threads to finish before disposing the server?

If so, you should take a look at a very detailed answer I got to a similar question that I asked some time ago. Many different approaches that you should consider are included in the answer.

The answer that was given is located here.

Hope that helps!

Community
  • 1
  • 1
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
  • yes, just to say thread.Join doesn't work in the test framework - no idea why. Anyhow, I'll have a good read on the link you sent tomorrow (very late here eyes closing and wife snoring) - thanks for the swift reply – Anthony Johnston Aug 14 '10 at 00:17
  • From this answer I will be using Join - thanks for the pointer – Anthony Johnston Aug 14 '10 at 11:54
0

I don't know much about .NET, but in java I would do this

10 create a server

20 for( a number of iterations ) start a thread running a function

30 while(all the threads are still doing stuff){
    MainThread.Sleep(xMiliseconds)
}

40 now dispose of the server

That way the MainThread will continually loop, sleep, loop, sleep, until all the threads are done, then it will dispose of the server.

strager
  • 88,763
  • 26
  • 134
  • 176
DRJTower
  • 585
  • 3
  • 8
  • 13
0

Join() is the easiest. If you can't do that, you can create an event for each thread and wait for the event to be signled. Alternatively, you could create a semaphore with an initial count for each thread and wait for all threads to release the semaphore.

Mike
  • 3,462
  • 22
  • 25