I'm starting a process in a thread that is used to built up a treeview with a recursive method. I'm getting the following error
"Action being performed on this control is being called from the wrong thread. marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action."
Here is where i start my thread: It's crashing on
"inTreeNode.Nodes.Add( tNode2 );" as if this was a reference directly to the original treeview, which isn't the case.
private void Init( XmlDocument xdoc )
{
treeView.Nodes.Clear();
treeView.Nodes.Add( new TreeNode( xdoc.DocumentElement.Name ) );
TreeNode tNode = new TreeNode();
tNode = treeView.Nodes[ 0 ];
Worker worker = new Worker( xdoc.DocumentElement , tNode );
worker.TaskComplete += new Worker.WorkerEventHandler( worker_TaskComplete );
Thread thread = new Thread( worker.Start );
thread.Start();
//AddNode( xdoc.DocumentElement , tNode );
treeView.Nodes[ 0 ].Expand();
}
Here is my worker definition.
class Worker
{
private bool _stop;
private int _count = 0;
private XmlNode _inXmlNode;
private TreeNode _inTreeNode;
public delegate void WorkerEventHandler( object sender , WorkerEventArgs e );
public event WorkerEventHandler TaskComplete;
public event WorkerEventHandler WorkComplete;
public void Start()
{
AddNode( _inXmlNode , _inTreeNode );
}
public Worker( XmlNode inXmlNode , TreeNode inTreeNode )
{
_inXmlNode = inXmlNode;
_inTreeNode = inTreeNode;
}
public void AddNode( XmlNode inXmlNode , TreeNode inTreeNode )
{
/*TaskComplete( this , new WorkerEventArgs( _count ) );
_count++;*/
try
{
XmlNode xNode;
XmlNodeList nodeList;
XmlElement xElement = (XmlElement)inXmlNode;
TreeNode tNode;
TreeNode tNode2;
//Serialization kit
Child objPage;
StringReader sr;
XmlSerializer serializer = new XmlSerializer( typeof( Child ) );
if( inXmlNode.HasChildNodes )
{
nodeList = inXmlNode.ChildNodes;
for( int i = 0 ; i < nodeList.Count - 1 ; i++ )
{
xNode = inXmlNode.ChildNodes[ i ];
xElement = (XmlElement)xNode;
tNode2 = new TreeNode( xElement.GetAttribute( "name" ) ); // will be the name of the node
//Serialize to later on save on the fly
sr = new StringReader( xNode.OuterXml );
objPage = (Child)serializer.Deserialize( sr );
tNode2.Tag = objPage;
inTreeNode.Nodes.Add( tNode2 );
tNode = inTreeNode.Nodes[ i ];
AddNode( xNode , tNode );
}
if( xElement.GetAttribute( "adopted" ) == "true" )
inTreeNode.ImageIndex = (int)Image.AdoptedFolder;
else
inTreeNode.ImageIndex = (int)Image.Folder;
}
else
{
inTreeNode.Text = xElement.GetAttribute( "name" ).Trim();
if( xElement.GetAttribute( "adopted" ) == "true" )
inTreeNode.ImageIndex = (int)Image.AdoptedPage;
else
inTreeNode.ImageIndex = (int)Image.Page;
}
}
catch( Exception ex )
{
MessageBox.Show( ex.Message , "Error" , MessageBoxButtons.OK , MessageBoxIcon.Error );
}
}
/// <summary>
/// Tells this worker to stop it's processing cycle
/// </summary>
public void Stop()
{
_stop = true;
}
}
Thanks for your help :) There might be some wrong stuff related to threading, I'm really new to that, feel free to comment.