Question

I have a fairly well multi-threaded winforms app that employs the EventWaitHandle in a number of places to synchronize access.

So I have code similar to this:

List<int> _revTypes;
EventWaitHandle _ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

void StartBackgroundTask() {
    _ewh.Reset();
    Thread t = new Thread(new ThreadStart(LoadStuff));
    t.Start();
}

void LoadStuff() {
    _revTypes = WebServiceCall.GetRevTypes()
    // ...bunch of other calls fetching data from all over the place
    // using the same pattern
    _ewh.Set();
}


List<int> RevTypes {
    get {
        _ewh.WaitOne();
        return _revTypes;
    }
}

Then I just call .RevTypes somewehre from the UI and it will return data to me when LoadStuff has finished executing.

All this works perfectly correctly, however RevTypes is just one property - there are actually several dozens of these. And one or several of these properties are holding up the UI from loading in a fast manner.

Short of placing benchmark code into each property, is there a way to see which property is holding the UI from loading? Is there a way to see whether the EventWaitHandle is forced to actually wait?

Was it helpful?

Solution

I'm not sure what and how you would like achieve, but you can cycle through the WaitHandles with a timeout of 0 to see if they are currently set, like:

class Program
{
    static void Main(string[] args)
    {
        EventWaitHandle[] waitHandles = new EventWaitHandle[3];
        for (int i = 0; i < 3; i++)
        {
            waitHandles[i] = new EventWaitHandle(false, EventResetMode.ManualReset);
        }
        waitHandles[1].Set();

        for (int i = 0; i < 3; i++)
        {
            if (waitHandles[i].WaitOne(0))
            {
                Console.WriteLine("Handle {0} is set", i);
            }
            else
            {
                Console.WriteLine("Handle {0} is not set", i);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top