That's the way I do it too.
In most of my public facing methods or methods that might be called by an event
handler that comes from a non-UI thread I made a code snippet to help me setup
a self-marshalling method (the snippet presumes it's in a form class):
Output of Snippet using , "RemoveFromBuddyList", "void", "string" and
"userName" as parameters:
private delegate void RemoveFromBuddyListDelegate(string userName);
private void RemoveFromBuddyList(string userName)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new
RemoveFromBuddyListDelegate(RemoveFromBuddyList), new object[] { userName });
}
else
{
}
}
Snippet:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>InvokeRequired</Title>
<Shortcut>UIMarshall</Shortcut>
<Description>Constructs delegate and method to have method marshal to UI
thread on Form class</Description>
<Author>Vince Pacella</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>MethodName</ID>
<ToolTip>Name of Method</ToolTip>
<Default>
</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>ReturnType</ID>
<ToolTip>Return type</ToolTip>
<Default>void</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>ParameterType</ID>
<ToolTip>Parameter Type</ToolTip>
<Default>
</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>ParameterName</ID>
<ToolTip>Parameter Name</ToolTip>
<Default>
</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private delegate $ReturnType$
$MethodName$Delegate($ParameterType$ $ParameterName$);
private $ReturnType$ $MethodName$($ParameterType$ $ParameterName$)
{
if(this.InvokeRequired)
{
this.BeginInvoke(new $MethodName$Delegate($MethodName$), new
object[]{$ParameterName$});
}
else
{
}
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
---
Vince
> -----Original Message-----
> From: Discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] On Behalf Of Brad Wilson
> Sent: Friday, May 19, 2006 10:25 PM
> To: [email protected]
> Subject: Re: [ADVANCED-DOTNET] Windows.Forms.Form + Threading
>
> In these scenarios, I actually tend to prefer that any
> Windows Forms code is the one that makes sure all calls from
> the outside world are done on the right thread.
>
> After all, it's a Windows Forms limitation. Why should the
> client have to be concerned with the inner requirements of
> yourself are, and pollute themselves with WinForms "magic"?
>
> On 5/19/06, Alexander Herrmann <[EMAIL PROTECTED]> wrote:
> >
> > But I recently found the solution or better the solution why this
> > wasn't working. When using Windows.Forms you have to
> explicitly invoke
> > the methods that should do their work in other threads than the UI
> > thread.
>
> ===================================
> This list is hosted by DevelopMentor http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com