1

After reading up on how to use Invoke to be able to update GUI elements from other threads I worked a bit with it, and has ended up with following approach to handle it. I am fairly certain I have overcomplicated the solution, but I do believe it is working as intended.

The advantages I see with this approach is, that it allows multiple commands to be stored in short succession for consumption once the GUI thread is ready for it, and the order of the commands are maintained. The downside is that to me storing these temporary parameters looks inefficient (I could create a generic class to store all to hide them to a single object)

I have chosen to reuse the same Mutex for all calls, but different ones could be used as long they are paired up.

So what other patterns could I use to get the same results? (hopefully in a less convoluted approach.)

//The main form class.
public class GUIHandler
{
    Mutex InvokeOnce = new Mutex(); //Mutex that ensures that temp storage only gets written to or read from

    //tempData for Invoke Methodes
    List<SomeObject> invokeParameter = new List<SomeObject>();
    List<SomeOtherObject> anotherInvokeParameter = new List<SomeOtherObject>();

    public GUIHandler()
    {
        //Some code to initialize the GUI
    }   

    //Generic Reused InvokeDelegate
    public delegate void InvokeDelegate();

    //External Call with parameters different calls different names
    private void SomeInvokeRequiredAction(SomeObject someParameter)
    {
        //call mutex for handle the storage and store the parameter
        InvokeOnce.WaitOne();
        invokeParameter.Add(someParameter);
        InvokeOnce.ReleaseMutex();
        this.BeginInvoke(new InvokeDelegate(SomeInvokeRequiredActionInvoke));
    }

    //Invoked Code with related name to its primary external call
    private void SomeInvokeRequiredActionInvoke()
    {
        InvokeOnce.WaitOne();
        //some random action on a GUI element that required the Invoke in first place
        guiElement.Text = invokeParameter[0]
        invokeParameter.RemoveAt(0);

        InvokeOnce.ReleaseMutex();
    }
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344
Taoh
  • 331
  • 2
  • 12
  • Using Invoke() instead of BeginInvoke() is less convoluted. No locking is required since only one thread can ever access the list at the same time. It is however certainly not more efficient. Invoking for a single data item at a time is usually a pretty bad idea, the odds for fire-hosing the UI thread are high. You've got only human eyes to entertain, they can't see anything beyond 25 updates per second. – Hans Passant Jan 11 '14 at 21:40
  • You may want to take a look at this question and it's answers: [Automating the InvokeRequired code pattern](http://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern) – Robert Groves Jan 11 '14 at 21:41
  • Typical I use lambdas with capture for storing variables for invocation. It's type safe and the class required to store the variable is created at compile time for you. – Aron Feb 01 '14 at 12:11

1 Answers1

1

If what you need is to:

  1. Pass work to the UI thread.
  2. Preserve order for that work.

You don't need much more than a simple Invoke. It is thread safe (so no locking is necessary). It searches for the topmost control and it's window handle and queues up work to run on it (message loop). And because it's only a single thread the work will be done in order.

As in the question that was suggested in comments, a simple solution like this one is probably enough:

Usage:

button.InvokeIfRequired(() =>
{
    // This will run under the UI thread.
    button.Text = "hamster";
});

Implementation:

public static void InvokeIfRequired(this ISynchronizeInvoke control, MethodInvoker action)
{
    if (control.InvokeRequired)
    {
        control.Invoke(action);
    }
    else
    {
        action();
    }
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344