How do I properly handle cross threading issues in my systray app?
So, I took over an existing WinForms app recently. We need to change it to something that runs in the system tray, but still pops up the form when a user wants to. No problem. As per this question: Mutex is being used to ensure only one copy of the app is running
The app has 3 main components: the custom application context (myContext) which handles all the system stray stuff. myContext creates an instance of a class called Gozer (myGozer). Gozer does all the real work. It performs a series of operations on a timed interval (check network status, if we have a connection it then does a few other things. It also plays Global Thermo-nuclear War with itself for fun). It let's everybody else know what's going on via a series of events. When myForm is opened, myContext is passing myGozer in.
It's when I open myForm that I start bumping into all sorts of Cross-Threading issues (any time a control is used in some way. Like manipulating myListView). What I'm not clear on is the best way to deal with this. I know very little about threading. I do not have a super large brain that can come to understand threading in 30 seconds. And I don't have anyone around I can eat to absorb their superior knowledge of threading.
According to this question, I can simply check someControl.InvokeRequired
and then invoke method in question via a delegate. This works. But now I'm facing dropping a bunch of code in front of every time I need to deal with a control, which sets off some likely poorly-calibrated alarm bells in my head. I also discovered a problem when exiting the application from the form; this causes another Cross-Threading exception back in myContext. I am not sure it's appropriate to exit the app from myForm any longer, but what other cross-threading madness is awaiting me at this time? I feel like I'm setting myself up for a lot of side-effect induced headaches.
I guess I'm really worried about creating a bunch of potential issues down the road. Or being asked to expand Gozer's capabilities and then creating further issues when that requires work in myForm. Or finding that opening and closing myForm while running creates a bunch of additional cross-threading issues. Or that the app will cause everything to explode.
Is there something else I should be considering or missing?
Note: this is for a .net 2.0 app, so Jethro's solution 开发者_如何学Pythonwill not work here. That said, since it's not that many places I have to write InvokeRequired logic, I'm just going to do that. I'm pretty sure I'm going to get to upgrade this to .net 3.5 in the next year and the suggested class below is how I'll be handling he issue. I'm marking it as the answer as a result.
When you do upgrade, there is an alternative if you don't want to write an extension method. You'd need to be comfortable with lambda syntax though. Here is an example:
myTextBox.Invoke( (Action) (() => myTextBox.Text = "text goes here"));
I usually don't check if the invoke is required and just do the invoke (you usually know if you are updating from a different thread than the UI's thread).
NOTE: I used to have a similar extension method, but got tired of creating it in all my projects or creating it in a library that needed to be added to all of my projects. This is a "simple" one liner way to do it.
Oh and if you need to do multiple operations you can also do that inline like so:
myListView.Invoke( (Action) (() =>
{
myListView.Columns.Add("Column 1", -2, HorizontalAlignment.Left);
myListView.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
myListView.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
myListView.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
}));
Here's an extension you can use. Can't recall were I picked it Up.
Call it like this.
this.InvokeEx(p=> p.txtbox.Text = "Rad");
public static class ControlExtensions
{
public static TResult InvokeEx<TControl, TResult>(this TControl control,
Func<TControl, TResult> func)
where TControl : Control
{
return control.InvokeRequired
? (TResult)control.Invoke(func, control)
: func(control);
}
public static void InvokeEx<TControl>(this TControl control,
Action<TControl> func)
where TControl : Control
{
control.InvokeEx(c => { func(c); return c; });
}
public static void InvokeEx<TControl>(this TControl control, Action action)
where TControl : Control
{
control.InvokeEx(c => action());
}
}
精彩评论