Domanda

Ho un WinForms multi-threaded app abbastanza bene che impiega l'EventWaitHandle in un certo numero di posti per sincronizzare l'accesso.

Quindi devo codice simile a questo:

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

Poi mi basta chiamare .RevTypes somewehre dall'interfaccia utente e sarà restituire i dati da me quando LoadStuff ha terminato l'esecuzione.

Tutto questo funziona perfettamente correttamente, tuttavia RevTypes è solo una proprietà - ci sono in realtà diverse decine di questi. E una o più di queste proprietà sono alzando la UI da carico in modo rapido.

A corto di collocare il codice di riferimento in ogni proprietà, c'è un modo per vedere quale proprietà sta tenendo l'interfaccia utente dal caricamento? Esiste un modo per vedere se l'EventWaitHandle è costretto ad aspettare in realtà?

È stato utile?

Soluzione

Non sono sicuro di cosa e come si vorrebbe raggiungere, ma è possibile scorrere le WaitHandles con un timeout di 0 per vedere se sono attualmente impostati, come:

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);
            }
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top