开发者

How to detect whether an EventWaitHandle is waiting?

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 Star开发者_如何学CtBackgroundTask() {
    _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?


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);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜