469,593 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Remoting and using the Invoke method to modify a form object

hi all,

I'm having a few problems whereby my application is hanging when using the
Invoke method of a form's control.

Basically, when a user clicks a button on the form, it calls a remote
function, which in turn fires an event that is caught by the form.
The form then need to add a value passed in the event to a form object,
however, when using the Invoke method, it just hangs.

Here's a snippet of the code;

public void NotifyUserAdded(string username)
{
TreeNodeCollection treeNodeCollection = treeMachines.Nodes[0].Nodes;
TreeNode treeNodeParent = treeMachines.Nodes[0];

AddDelegate addDelegate = new AddDelegate(treeNodeCollection.Add);
ExpandDelegate expandNodeDelegate = new
ExpandDelegate(treeNodeParent.Expand);

treeMachines.Invoke(addDelegate, new object[] {new
TreeNode(username,0,0)});
treeMachines.Invoke(expandNodeDelegate);

}

the two delegates are obviously previously defined.
delegate int AddDelegate(TreeNode treeNode);
delegate void ExpandDelegate();

Sometimes it works, but it's very slow. If i take the invoke out and use
the Node.Add method, it works... but obviously not correctly - as 50% of the
time it throws the "must use invoke" exception.

Any ideas?

TIA

Sam Martin

Nov 15 '05 #1
1 4552
Hi,

If I read this correctly the NotifyUserAdded is the event that is
triggered by the remote call. This means the event will be called on a
thread which is not the UI thread (the thread on which the control/component
was constructed). Any calls to UI objects may hang if they are made in a
thread which is not the UI thread. It may also be that because you have
used Invoke which blocks and not BeginInvoke which does not.

I would suggest a slightly different model:

public delegate void NotifyUserAddedEventHandler( string username );
public void NotifyUserAdded( string username )
{
if( this.InvokeRequired == true )
{
// From memory so this may be wrong syntax!.
this.BeginInvoke( new NotifyUserAddedEventHandler(
this.NotifyUserAdded ), new object[] { username } );
}
else
{
// Do your UI stuff here without any need to invoke - just as
normal.
}
}

Of course this is just one way of doing things. Hopefully this will solve
your hanging issue.

Regards,

- Bruce.


"boxim" <sa*********@yahoo.co.uk> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
hi all,

I'm having a few problems whereby my application is hanging when using the
Invoke method of a form's control.

Basically, when a user clicks a button on the form, it calls a remote
function, which in turn fires an event that is caught by the form.
The form then need to add a value passed in the event to a form object,
however, when using the Invoke method, it just hangs.

Here's a snippet of the code;

public void NotifyUserAdded(string username)
{
TreeNodeCollection treeNodeCollection = treeMachines.Nodes[0].Nodes;
TreeNode treeNodeParent = treeMachines.Nodes[0];

AddDelegate addDelegate = new AddDelegate(treeNodeCollection.Add);
ExpandDelegate expandNodeDelegate = new
ExpandDelegate(treeNodeParent.Expand);

treeMachines.Invoke(addDelegate, new object[] {new
TreeNode(username,0,0)});
treeMachines.Invoke(expandNodeDelegate);

}

the two delegates are obviously previously defined.
delegate int AddDelegate(TreeNode treeNode);
delegate void ExpandDelegate();

Sometimes it works, but it's very slow. If i take the invoke out and use
the Node.Add method, it works... but obviously not correctly - as 50% of the time it throws the "must use invoke" exception.

Any ideas?

TIA

Sam Martin

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Sunny | last post: by
1 post views Thread by Bruce M. Carroll | last post: by
4 posts views Thread by GTi | last post: by
1 post views Thread by Thomee Wright | last post: by
2 posts views Thread by Sagaert Johan | last post: by
4 posts views Thread by Rich | last post: by
7 posts views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.