
Add a node to treeview under thread
Hi,
I am having exactly the same problem (In VB.NET).
I tried ot in the same way, and made it as you described here.
But I now get another exception:
An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an
invocation.
I was not able to find some usefull information regarding this
exception.
Maybe it would be very helpfully if you could send a small working
sample into this group.
Thank you,
Andre Lorbach
Quote:
> > Welson,
> > I should have clairified. There is an Invoke method on the Control
> > class (and every class that derives from it), that will call the method on
> > the appropriate thread. You don't want to call the InvokeMember method on
> > the type.
> > --
> > - Nicholas Paldino [MVP]
> > > yes,but I use the invoke ,it give me another error.
> > > object [] obj = new object[] {objNode};
> > > Type t=treeView1.GetType();
> t.InvokeMember("Add",System.Reflection.BindingFlags.Default|System.Reflectio
> > > n.BindingFlags.InvokeMethod,null,treeView1.Nodes,obj);
> > > the error :An unhandled exception of type 'System.MissingMethodException'
> > > occurred in mscorlib.dll
> > > Additional information: Attempted to access a missing member.
> > > > Welson,
> > > > Any calls made to a class that represents a UI element should be
> > made
> > on
> > > > the same thread that the element was created on. You are probably
> getting
> > > > these errors for that reason.
> > > > You should use the Invoke method to make the call you want from
> another
> > > > thread.
> > > > --
> > > > - Nicholas Paldino [MVP]
> > > > > help me!!
> > > > > I want to add a node use the following code
> > > > > private button1_click(object sender, System.EventArgs e)
> > > > > {
> > > > > Thread th1=new Thread(new ThreadStart(DoWith));
> > > > > th1.Start();
> > > > > }
> > > > > private void DoWith()
> > > > > {
> > > > > TreeNode objNode=new TreeNode("This is only a test");
> > > > > treeView1.Nodes.Add(objNode);
> > > > > }
> > > > > its give me error;but when I add text to TextBox and add a item to
> Combox,
> > > > > it works well.
> > > > > I want to know does there any diferrence with them.
> > > > > it's the error:
> > > > > An unhandled exception of type 'System.InvalidOperationException'
> occurred
> > > > > in system.windows.forms.dll
> > > > > Additional information: The action being performed on this control is
> being
> > > > > called from the wrong thread. You must marshal to the correct thread
> using
> > > > > Control.Invoke or Control.BeginInvoke to perform this action.
> > > > > welson