I am running into a bit of difficulty using the threadpool class and an array of ManualResetEvents. Below is a simple example of what I am doing. The problem is that in the DoWork method I am getting null references to the resetEvent[param as int] object.
Can't seem to figure what I'm doing wrong.
(edit: got the code block working)
private static volatile ManualResetEvent[] resetEvents = new ManualResetEvent[NumThreads]; public void UpdateServerData() { for (int i = 0; i < NumThreads ; i++) { resetEvents[i] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), (object) i); } WaitHandle.WaitAll(resetEvents); } private void DoWork(object param) { //do some random work resetEvents[(int)param].Set(); } EDIT: I have tried inserting a System.Threading.Thread.MemoryBarrier(); after each .Set() however i still get a null reference exception.
Set()? You need to see the updated array element before you callSet()on it!RegisteredWaitHandle.Unregisterafter using the thread, as advised by msdn, unlike in the code here provided.