Home All Groups Group Topic Archive Search About

Worker thread is blocking UI !!!

Author
14 Mar 2006 3:23 PM
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

Author
14 Mar 2006 5:08 PM
Nick Hounsome
Show quote
"Gancy" <ganesh_tiw***@hotmail.com> wrote in message
news:1142349796.099076.55000@p10g2000cwp.googlegroups.com...
> 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?

"tvwProperties.Invoke(StdMIBTree)" is causing StdMIBTree to actually do all
the work on the UI thread - that is what it is for!

Build a root node (with subnodes) in the worker thread but don't add it to
the view.
THEN use tvwProperties.Invoke to pass the constructed  node back to the UI
thread to actually add it to the view (you'll need another delegate/method).

Alternatively build the tree piece by piece using Invoke to add each node
individually.

Also dont forget to use BeginEdit and EndEdit where appropriate.
Author
15 Mar 2006 12:26 AM
Jeroen-bart Engelen
Also remember that Invoke() blocks until it is complete. If you want to
prevent blocking, use BeginInvoke() and EndInvoke().
Author
15 Mar 2006 8:06 AM
Nick Hounsome
"Jeroen-bart Engelen" <digi***@gmail.com> wrote in message
news:1142382409.194665.12810@j52g2000cwj.googlegroups.com...
> Also remember that Invoke() blocks until it is complete. If you want to
> prevent blocking, use BeginInvoke() and EndInvoke().

True but that's not his problem - he is blocking the worker to do work on
the UI thread when his whole reason for having a worker was that it should
do the work.

AddThis Social Bookmark Button