问题
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();
}
}
回答1:
If what you need is to:
- Pass work to the UI thread.
- 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();
}
}
来源:https://stackoverflow.com/questions/21068083/using-invoke-to-handle-calls-from-other-threads-is-this-a-good-pattern