I have what I assume is a very simple threading synchronization problem that I just can't figure out.
I have several forms that make up the UI that needs to be responsive. I also receive many updates from a background thread (100's per second) that sends data to the various forms. I use a handler that keeps a list of all the forms. Here's some psuedo code:
Handler.Update(Object Data)
{
lock(FormsLock)
{
for(int i=0; i < Forms.Count; i++)
Forms.AcceptData(Data);
}
}
The following function updates the UI so I invoke it on the owner's thread.
Form.AcceptData(Object Data)
{
if(this.InvokeRequired)
this.Invoke(AcceptDataDelegate, Data);
else
// Do work...
}
That code works fine by itself. But say I want to add another form to the collection that gets updated like this:
Handler.AddForm(Form NewForm)
{
lock(FormsLock)
{
Forms.Add(NewForm);
}
}
It's important to note that Handler.AddForm() is being called by one of the forms being updated. It's easy to see that if I'm getting 100's of updates that I am almost guaranteed a deadlock within a second. Any suggestions
Thanks in advance for the help.