My WinForms application connects to a running service via WCF and subscribes to events from the service. When the event fires, my application requests current data from the service and calls a method named UpdateDisplay() to populate a DataGridView and display it. The grid view is remaining empty, and I'm trying to debug it. But if I put a breakpoint inside UpdateDisplay(), the next time the event fires, I get a deadlock because the first event handler hasn't finished yet.
I thought I could unsubscribe from the event at the top of my method and resubscribe to it at the bottom, something like this:
private static bool m_updatingDisplay = false;
private void UpdateDisplay()
{
// Unsubscribe from the event while the display is being updated. This should remove the problem of deadlocks while debugging because
// the event gets raised again.
m_callbackHandler.DatabaseUpdatedEvent -= m_callbackHandler_DatabaseUpdatedEvent;
DoSomethingThatTakesALongTime();
m_callbackHandler.DatabaseUpdatedEvent += m_callbackHandler_DatabaseUpdatedEvent;
}
But I still get the exception.
My only thought is to have the event handler merely set a flag, and then a timer would check the flag every minute or so, and if it's true, then it would do the update and clear the flag. I think that would work, but I have a feeling this is a common problem with a well-recognized solution, and I should probably know what it is. What's the best way to handle this situation?