Make UI method threadsafe

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
1 comment
Comment actions Permalink

Hello,

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:


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!”


0

Please sign in to leave a comment.