Consider the following code in which i add nodes to a treeview control by reading an MS Access database.
Obviously, if I execute in a seperate thread I will get exception about control accessed from a thread other than the one in which it is created.
Now whenever I am adding one node to the tree I could marshel it to the GUI thread like this
The problem is that I have got some nodes which are parents of child nodes.
So we create a node and add all children of it and finally add it to treeview.
If i create the children in worker thread and send the parent to GUI thread to add it to the treeview I guess I wont be able to access those children from GUI thread(coz they are cerated in the worker thread)
How would i get around this problem?
Obviously, if I execute in a seperate thread I will get exception about control accessed from a thread other than the one in which it is created.
Now whenever I am adding one node to the tree I could marshel it to the GUI thread like this
Code:
void addnode(string textofnode)
{
}
So we create a node and add all children of it and finally add it to treeview.
If i create the children in worker thread and send the parent to GUI thread to add it to the treeview I guess I wont be able to access those children from GUI thread(coz they are cerated in the worker thread)
How would i get around this problem?
Code:
TreeNode extensions = new TreeNode("Extensions");
treeexchange.Nodes.Add(extensions);
DataTable dtgroups = new DataTable("Groups"); //get groups
OleDbDataAdapter adp = new OleDbDataAdapter("select * from t_extensiongroups", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
adp.Fill(dtgroups);
for (int i = 0; i < dtgroups.Rows.Count; i++)//for all the groups
{
//create a groupnode with the group name in it
TreeNode group = new TreeNode(dtgroups.Rows[i][0].ToString());
//get no of extensions corresposnding to that group
adp = new OleDbDataAdapter("select * from t_extensions where group_name='" + dtgroups.Rows[i]["groups"].ToString() + "'", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
DataTable dtextensions = new DataTable("Extensions");
adp.Fill(dtextensions);
//add all the extension nodes to the groupnode
for (int j = 0; j < dtextensions.Rows.Count; j++)
{
group.Nodes.Add(dtextensions.Rows[j]["name"].ToString() + " (" + dtextensions.Rows[j]["number"].ToString() + ")");
}
//add the group
extensions.Nodes.Add(group);
}
//create a trunk node
TreeNode trunks = new TreeNode("Trunks");
//get trunks
adp = new OleDbDataAdapter("select connected_trunk,name from t_trunk_lines", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
DataTable dttrunks = new DataTable("Trunks");
adp.Fill(dttrunks);
//add all trunks to the trunk node
for (int i = 0; i < dttrunks.Rows.Count; i++)
{
trunks.Nodes.Add(dttrunks.Rows[i]["name"].ToString() + " (" + dttrunks.Rows[i]["connected_trunk"].ToString() + ")");
}
//add the trunk node
treeexchange.Nodes.Add(trunks);
//add the pbx node
treeexchange.Nodes.Add("My PBX");
treeexchange.Nodes.Add("User");
Comment