Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Treeview in a thread [modified]

Treeview in a thread [modified]

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronni Marker
    wrote on last edited by
    #1

    Hi all, I am trying to populate a treeview from a webservice through a thread and get an invoke error when I call the function in the thread - outside the thread it works perfectly - but is too slow to be used outside a thread. So wondered if there were anyone who have some experience on Invoking in treeviews? my code is as follows:

        private void ThreadingGroups()
        {
            Thread TsGroups = new Thread(ThreadGroups);
            TsGroups.Start();
        }
    
        private void ThreadGroups()
        {
            DataTable dt = DOCTemp.GetGroups();
            DataRow dr = dt.NewRow();
            dr\[0\] = "";
            dr\[1\] = "Default";
            dt.Rows.InsertAt(dr, 0);
            frmTemplateManager.treeView1.Nodes.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TemplateManagerTreeView(dt.Rows\[i\]\["id"\].ToString(), dt.Rows\[i\]\["name"\].ToString());
            }
        }
    
        private void TemplateManagerTreeView(string id, string name)
        {
            frmTemplateManager.treeView1.Nodes.Add(id.ToString(), name.ToString());
        }
    

    The function is basically updating a treeview located in form named frmTemplateManager and it iself it works quite well. Just need to find a way to get the Invoke to work in a way.. Cheers,

    modified on Sunday, October 25, 2009 3:17 AM

    R N L 3 Replies Last reply
    0
    • R Ronni Marker

      Hi all, I am trying to populate a treeview from a webservice through a thread and get an invoke error when I call the function in the thread - outside the thread it works perfectly - but is too slow to be used outside a thread. So wondered if there were anyone who have some experience on Invoking in treeviews? my code is as follows:

          private void ThreadingGroups()
          {
              Thread TsGroups = new Thread(ThreadGroups);
              TsGroups.Start();
          }
      
          private void ThreadGroups()
          {
              DataTable dt = DOCTemp.GetGroups();
              DataRow dr = dt.NewRow();
              dr\[0\] = "";
              dr\[1\] = "Default";
              dt.Rows.InsertAt(dr, 0);
              frmTemplateManager.treeView1.Nodes.Clear();
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  TemplateManagerTreeView(dt.Rows\[i\]\["id"\].ToString(), dt.Rows\[i\]\["name"\].ToString());
              }
          }
      
          private void TemplateManagerTreeView(string id, string name)
          {
              frmTemplateManager.treeView1.Nodes.Add(id.ToString(), name.ToString());
          }
      

      The function is basically updating a treeview located in form named frmTemplateManager and it iself it works quite well. Just need to find a way to get the Invoke to work in a way.. Cheers,

      modified on Sunday, October 25, 2009 3:17 AM

      R Offline
      R Offline
      Ronni Marker
      wrote on last edited by
      #2

      btw I did have a look at the article "Populating TreeView on a Background Thread" but didnt give me any clues on how i got out of my error.

      1 Reply Last reply
      0
      • R Ronni Marker

        Hi all, I am trying to populate a treeview from a webservice through a thread and get an invoke error when I call the function in the thread - outside the thread it works perfectly - but is too slow to be used outside a thread. So wondered if there were anyone who have some experience on Invoking in treeviews? my code is as follows:

            private void ThreadingGroups()
            {
                Thread TsGroups = new Thread(ThreadGroups);
                TsGroups.Start();
            }
        
            private void ThreadGroups()
            {
                DataTable dt = DOCTemp.GetGroups();
                DataRow dr = dt.NewRow();
                dr\[0\] = "";
                dr\[1\] = "Default";
                dt.Rows.InsertAt(dr, 0);
                frmTemplateManager.treeView1.Nodes.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TemplateManagerTreeView(dt.Rows\[i\]\["id"\].ToString(), dt.Rows\[i\]\["name"\].ToString());
                }
            }
        
            private void TemplateManagerTreeView(string id, string name)
            {
                frmTemplateManager.treeView1.Nodes.Add(id.ToString(), name.ToString());
            }
        

        The function is basically updating a treeview located in form named frmTemplateManager and it iself it works quite well. Just need to find a way to get the Invoke to work in a way.. Cheers,

        modified on Sunday, October 25, 2009 3:17 AM

        N Offline
        N Offline
        Nicholas Butler
        wrote on last edited by
        #3

        As you have found out, you must always use a UI control on the main thread. So you will have to marshal the treeView1.Nodes.Add calls on to the main thread using Invoke or BeginInvoke. What's taking the time - is it DOCTemp.GetGroups() or the loop round TemplateManagerTreeView? If it's the GetGroups call, then you can just marshal the whole loop in one go. If it's actually adding the nodes, you will have to split the loop up into chunks and marshal them one after the other. Invoking every TemplateManagerTreeView call individually would be slow. Nick

        ---------------------------------- Be excellent to each other :)

        R 1 Reply Last reply
        0
        • N Nicholas Butler

          As you have found out, you must always use a UI control on the main thread. So you will have to marshal the treeView1.Nodes.Add calls on to the main thread using Invoke or BeginInvoke. What's taking the time - is it DOCTemp.GetGroups() or the loop round TemplateManagerTreeView? If it's the GetGroups call, then you can just marshal the whole loop in one go. If it's actually adding the nodes, you will have to split the loop up into chunks and marshal them one after the other. Invoking every TemplateManagerTreeView call individually would be slow. Nick

          ---------------------------------- Be excellent to each other :)

          R Offline
          R Offline
          Ronni Marker
          wrote on last edited by
          #4

          Well it do take some time for the Webservice to finish. Especially when my server is located in California and some users will access this application in New Zealand - and that can be far, far, far away when im messing around with applications that don't play well with latency. I am not asking you to do the code for me, but do you have some examples on how to marshal the loop when using the Invoke or BeginInvoke? Cheers, Ronni

          N 1 Reply Last reply
          0
          • R Ronni Marker

            Well it do take some time for the Webservice to finish. Especially when my server is located in California and some users will access this application in New Zealand - and that can be far, far, far away when im messing around with applications that don't play well with latency. I am not asking you to do the code for me, but do you have some examples on how to marshal the loop when using the Invoke or BeginInvoke? Cheers, Ronni

            N Offline
            N Offline
            Nicholas Butler
            wrote on last edited by
            #5

            This is an example for WinForms. WPF is similar, but uses the Dispatcher class.

            private void UpdateTreeView( DataTable dt )
            {
            for ( int i = 0 ; i < dt.Rows.Count ; i++ )
            {
            TemplateManagerTreeView(dt.Rows[i]["id"].ToString(), dt.Rows[i]["name"].ToString());
            }
            }

            Then in ThreadGroups() put:

            frmTemplateManager.treeView1.Invoke( ( Action<DataTable> ) UpdateTreeView, dt );

            Nick

            ---------------------------------- Be excellent to each other :)

            R 1 Reply Last reply
            0
            • R Ronni Marker

              Hi all, I am trying to populate a treeview from a webservice through a thread and get an invoke error when I call the function in the thread - outside the thread it works perfectly - but is too slow to be used outside a thread. So wondered if there were anyone who have some experience on Invoking in treeviews? my code is as follows:

                  private void ThreadingGroups()
                  {
                      Thread TsGroups = new Thread(ThreadGroups);
                      TsGroups.Start();
                  }
              
                  private void ThreadGroups()
                  {
                      DataTable dt = DOCTemp.GetGroups();
                      DataRow dr = dt.NewRow();
                      dr\[0\] = "";
                      dr\[1\] = "Default";
                      dt.Rows.InsertAt(dr, 0);
                      frmTemplateManager.treeView1.Nodes.Clear();
                      for (int i = 0; i < dt.Rows.Count; i++)
                      {
                          TemplateManagerTreeView(dt.Rows\[i\]\["id"\].ToString(), dt.Rows\[i\]\["name"\].ToString());
                      }
                  }
              
                  private void TemplateManagerTreeView(string id, string name)
                  {
                      frmTemplateManager.treeView1.Nodes.Add(id.ToString(), name.ToString());
                  }
              

              The function is basically updating a treeview located in form named frmTemplateManager and it iself it works quite well. Just need to find a way to get the Invoke to work in a way.. Cheers,

              modified on Sunday, October 25, 2009 3:17 AM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I would suggest you read this[^]. :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              R 1 Reply Last reply
              0
              • N Nicholas Butler

                This is an example for WinForms. WPF is similar, but uses the Dispatcher class.

                private void UpdateTreeView( DataTable dt )
                {
                for ( int i = 0 ; i < dt.Rows.Count ; i++ )
                {
                TemplateManagerTreeView(dt.Rows[i]["id"].ToString(), dt.Rows[i]["name"].ToString());
                }
                }

                Then in ThreadGroups() put:

                frmTemplateManager.treeView1.Invoke( ( Action<DataTable> ) UpdateTreeView, dt );

                Nick

                ---------------------------------- Be excellent to each other :)

                R Offline
                R Offline
                Ronni Marker
                wrote on last edited by
                #7

                Man, so simple and yet so beautiful... Thanks buddy. Btw. implemented it but found out that when running it in a thread it gets added trice. Running it outside the thread will only added to the treeview once as it should. Been going over the code but couldnt really see anything that should trigger the thread 3 times, so wondered if that had something to do with the invoke function?

                1 Reply Last reply
                0
                • L Luc Pattyn

                  I would suggest you read this[^]. :)

                  Luc Pattyn


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  R Offline
                  R Offline
                  Ronni Marker
                  wrote on last edited by
                  #8

                  Thanks!, will read it. Can see that the more I get to know about C#, the more I have to read up on. *Damn*. :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups