none
System.Threading.Thread RRS feed

  • Question

  • Hi,

    I have a method:

    private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) {  }

    which is the BeforeExpand event of a treeView, I need to execute the body of this method with a Thread but the problem is that I need the argument: 'e'

    Why do I need 'e'?

      Basically this method searches a directory, address is stored in the Tag of the expanded Node, and add sub-nodes to the expanded node based on the files and folders in the directory.

    What should I do?

    Thank you

    Thursday, May 30, 2013 6:44 AM

Answers

  • Consider a parameterized thread. See how a parameter is passed: http://msdn.microsoft.com/en-us/library/1h2f2459.aspx. Instead of “42” you can pass e or e.Node.

    Inside the thread procedure you have to cast the received object to required type.

    • Edited by Viorel_MVP Thursday, May 30, 2013 11:47 AM
    • Marked as answer by Kamyar92 Thursday, May 30, 2013 2:57 PM
    Thursday, May 30, 2013 11:46 AM
  • Probably the X part contains things that cannot be executed inside parallel threads. For example you cannot add new nodes with ‘myNode.Nodes.Add(“My Node”)’. You have to execute ‘BeginInvoke(new Action(()=>myNode.Nodes.Add("My Node")))’.

    Try finding the place where it stops, for example intercept the exceptions inside the thread with try-catch.

    • Marked as answer by Kamyar92 Thursday, May 30, 2013 2:56 PM
    Thursday, May 30, 2013 1:03 PM

All replies

  • Consider a parameterized thread. See how a parameter is passed: http://msdn.microsoft.com/en-us/library/1h2f2459.aspx. Instead of “42” you can pass e or e.Node.

    Inside the thread procedure you have to cast the received object to required type.

    • Edited by Viorel_MVP Thursday, May 30, 2013 11:47 AM
    • Marked as answer by Kamyar92 Thursday, May 30, 2013 2:57 PM
    Thursday, May 30, 2013 11:46 AM
  • Thanks for the reply

    I tried in this way:

    -------------------------------------------------------------------------------------------------------------------

        private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)

        {

            System.Threading.Thread thread = new System.Threading.Thread(treeView1Updater);
            thread.Start(e);

        }

        private void treeView1Updater(object o)

        {

            System.Windows.Forms.TreeViewCancelEventArgs e = (System.Windows.Forms.TreeViewCancelEventArgs)o;

            // I moved all codes of 'treeView1_BeforeExpand' method here... (the X part)

        }

    -------------------------------------------------------------------------------------------------------------------

    But just after expanding a node, app stops working! (app works fine before I change the codes, I mean the X part is fine) Am I trying in the correct way?

    • Marked as answer by Kamyar92 Thursday, May 30, 2013 2:55 PM
    • Unmarked as answer by Kamyar92 Thursday, May 30, 2013 2:56 PM
    Thursday, May 30, 2013 12:53 PM
  • Probably the X part contains things that cannot be executed inside parallel threads. For example you cannot add new nodes with ‘myNode.Nodes.Add(“My Node”)’. You have to execute ‘BeginInvoke(new Action(()=>myNode.Nodes.Add("My Node")))’.

    Try finding the place where it stops, for example intercept the exceptions inside the thread with try-catch.

    • Marked as answer by Kamyar92 Thursday, May 30, 2013 2:56 PM
    Thursday, May 30, 2013 1:03 PM
  • Worked!

    Thanks a lot...

    Thursday, May 30, 2013 2:56 PM