Make UI method threadsafe Follow
I have to port a large app from Net 1.1 to 2.0. 2.0 is much stricter with checking crossthread calls to UI methods. So you want to replace things like:
public TreeControl GetTree()
{
return tree;
}
by
private delegate TreeControl GetTreeDelegate();
public TreeControl GetTree()
{
if (InvokeRequired)
return (Crownwood.DotNetMagic.Controls.TreeControl) (this.Invoke(new GetTreeDelegate(GetTree)));
return tree;
}
Attached is a context action plugin that does exactly that.
Perhaps a suggestion to incorporate this as a Context Action or Refactoring in a future release of Resharper?
Amos Wittenberg
Attachment(s):
MakeMethodThreadSafeAction.zip
Please sign in to leave a comment.
Hello,
Assuming that “tree” is a quasi-readonly WinForms field, the only difference
I can see between these two code samples is that the second one is deadlock-prone.
Maybe you rather meant calling a method of the tree in the code sample?
—
Serge Baltic
JetBrains, Inc — http://www.jetbrains.com
“Develop with pleasure!”