Novice C# threading: WaitHandles

To safely start and stop code running in a background thread, an array of WaitHandles (one each for the start and stop events) and the WaitHandle.WaitAny method is often very handy. For example:

WaitHandles.WaitAny() blocks on an array of WaitHandles, and when one is set it returns the index of the WaitHandle that received the signal. I find this particularly handy when creating a stress test, since this is a safe way to create a number of threads and start/stop them all simultaneously with the same WaitHandles.

It’s fairly straightforward to queue up activity for execution in the background thread, a simple pattern for which I’ll describe in a later post.


While in Tampa, I had the pleasure of dining at Roy’s one night. I highly recommend everything I had, from the Hawaiian martini (or two), the tiger shrimp salad (interesting presentation, considering it’s a regular lettuce-based salad but still shaped sort of like a bundt cake), to something like swordfish that I can’t remember. I had no idea that a gorgonzola-based sauce would even be edible on fish, much less amazing. I almost feel guilty that I didn’t try the creme brulee, but the famous chocolate soufflĂ© definitely helped.

I look forward to going back with a sushi fan.