Trevor Hickey
Trevor Hickey

Reputation: 37914

Can InvokeIfRequired be used on an entire form before calling the form's method?

In Winforms,
I have a non-GUI thread listening for incoming UDP messages.
When it receives a message, it update various UI components on a form by calling one of the form's methods.

This is apparently a race condition.
It seems that I am suppose to use some kind of InvokeIfRequired pattern on every single component.

Can I just get a single lock, instead of litering my code with hundreds of these InvokeIfRequired conditionals and lambdas everywhere that people seem to suggest? Can I introduce a thread safe adjustment around the single call to the form, instead of having to modify the internals of the form code?

My running thread looks something like this:

private static void HandleMessages(frmGui gui)
{
   while(true){
      if (/*udp message found*/){
          gui.UpdateLotsOfGuiComponents();
      }
   }
}

And the form looks something like this:

public partial class frmGui : Form
{
    public void UpdateLotsOfGuiComponents()
    {
        //lots of UI changes through a large call stack
    }
}

I was hoping to just put something around my call to gui.UpdateLotsOfGuiComponents();.

Upvotes: 1

Views: 496

Answers (0)

Related Questions