Worker thread is blocking UI !!!

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
 
J

Jon Skeet [C# MVP]

Gancy said:
I am trying to add nodes into a treeview control (through recursion)
from a secondary thread.

You're not actually doing much in the secondary thread at all - you're
just calling back to the UI thread here:

if (tvwProperties.InvokeRequired)
{
ThreadSafeCallback StdMIBTree = new
ThreadSafeCallback(BuildMIBTree);
tvwProperties.Invoke(StdMIBTree);

That call to Invoke will be marshalling back to the UI thread.

Jon
 
G

Gancy

I thought this would work until i executed the code :)
then i came to know that secondary thread is marshaling the work to UI
thread.

Could you please give me a hint as what i could do in secondary thread
free-up the main thread?
 
J

Jon Skeet [C# MVP]

Gancy said:
I thought this would work until i executed the code :)
then i came to know that secondary thread is marshaling the work to UI
thread.

Could you please give me a hint as what i could do in secondary thread
free-up the main thread?

Well all of the work is actually manipulating the UI, isn't it? That
*has* to be done within the UI thread. You could separate it into small
batches, or if you can *prepare* the data in a different thread, then
do just the UI manipulation in the UI thread, that would help.

Jon
 
G

Gancy

Yup. sound good. I could prepare the data on on different thread and
allow UI thread just to update its controls
Thank you very mcuh for help

- Gancy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top