How to determine if EventWaitHandle is expecting?

I have a pretty good multi-threaded winforms application that uses EventWaitHandle in several 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 .RevTypessomeehre from the user interface and it will return the data to me when it LoadStuffcompletes.

All this works perfectly correctly, however RevTypes- this is just one property - in fact there are several dozen of them. And one or more of these properties quickly support the user interface.

, , ? , EventWaitHandle ?

+3
1

, , WaitHandle - 0, , , :

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);
            }
        }
    }
}
+3

Source: https://habr.com/ru/post/1736732/


All Articles