Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Worker thread is blocking UI !!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gancy - 14 Mar 2006 16:23 GMT
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
Nick Hounsome - 14 Mar 2006 18:08 GMT
> Hi,
> I am trying to add nodes into a treeview control (through recursion)
[quoted text clipped - 59 lines]
> 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.
Jeroen-bart Engelen - 15 Mar 2006 01:26 GMT
Also remember that Invoke() blocks until it is complete. If you want to
prevent blocking, use BeginInvoke() and EndInvoke().
Nick Hounsome - 15 Mar 2006 09:06 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.