G
Gancy
Hi,
I am trying to add nodes into a treeview control (through recursion)
from a secondary thread. Following is the code snippet
class Test
{
private delegate void ThreadSafeCallback();
private System.Threading.Thread thrMIBTree;
private System.Threading.ThreadStart startMIBTree;
// Class constructor
public Test()
{
startMIBTree = new
System.Threading.ThreadStart(BuildMIBTree);
thrMIBTree = new System.Threading.Thread(startMIBTree);
}
void BuildMIBTree()
{
// This if condition is to allow the marhsalling, so
that the thread which
// created the control actually adds nodes to it.
if (tvwProperties.InvokeRequired)
{
ThreadSafeCallback StdMIBTree = new
ThreadSafeCallback(BuildMIBTree);
tvwProperties.Invoke(StdMIBTree);
}
else
{
objSNMPManager.CompileMibs(this.m_sMIBPath);
foreach (Variables snmpVar in
objSNMPManager.Variables)
AddNode(snmpVar.Oid);
}
}
private void AddNode(string OID)
{
string parentOID = ParentNode(OID);
TreeNode[] nd = tvwProperties.Nodes.Find(parentOID, true);
if (nd.Length == 0)
AddNode(parentOID);
TreeNode newNode = new TreeNode();
newNode.Name = OID;
newNode.Text = GetDescription(OID);
nd = tvwProperties.Nodes.Find(parentOID, true);
nd[0].Nodes.Add(newNode);
}
private void toolButtonProps_Click(object sender, EventArgs e)
{
thrMIBTree.Start();
}
}
What i dont understand is why call to thrMIBTree.Start() is blocking
the UI? any better method to do this?
Thanks
- Gancy
I am trying to add nodes into a treeview control (through recursion)
from a secondary thread. Following is the code snippet
class Test
{
private delegate void ThreadSafeCallback();
private System.Threading.Thread thrMIBTree;
private System.Threading.ThreadStart startMIBTree;
// Class constructor
public Test()
{
startMIBTree = new
System.Threading.ThreadStart(BuildMIBTree);
thrMIBTree = new System.Threading.Thread(startMIBTree);
}
void BuildMIBTree()
{
// This if condition is to allow the marhsalling, so
that the thread which
// created the control actually adds nodes to it.
if (tvwProperties.InvokeRequired)
{
ThreadSafeCallback StdMIBTree = new
ThreadSafeCallback(BuildMIBTree);
tvwProperties.Invoke(StdMIBTree);
}
else
{
objSNMPManager.CompileMibs(this.m_sMIBPath);
foreach (Variables snmpVar in
objSNMPManager.Variables)
AddNode(snmpVar.Oid);
}
}
private void AddNode(string OID)
{
string parentOID = ParentNode(OID);
TreeNode[] nd = tvwProperties.Nodes.Find(parentOID, true);
if (nd.Length == 0)
AddNode(parentOID);
TreeNode newNode = new TreeNode();
newNode.Name = OID;
newNode.Text = GetDescription(OID);
nd = tvwProperties.Nodes.Find(parentOID, true);
nd[0].Nodes.Add(newNode);
}
private void toolButtonProps_Click(object sender, EventArgs e)
{
thrMIBTree.Start();
}
}
What i dont understand is why call to thrMIBTree.Start() is blocking
the UI? any better method to do this?
Thanks
- Gancy