invoke required keeps true

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Meganutter
    New Member
    • Mar 2009
    • 47

    invoke required keeps true

    Hello all, i am making a multithreaded program which copies a file.
    at the end of the file copy it will call a process to update a certain dropdownlist.
    the following code will cause a stack overflow, i copied it from the MSDN library and took out the items i didnt need (parameters) and replaced it with my variables.

    any help would be appreciated, it could well be that i overlooked something

    Code:
    delegate void LoadList();
    
    private void loadList()
            {
                if (ddlTools.InvokeRequired)
                {
                    LoadList d = new LoadList(loadList);
                    d.Invoke();
                }
                else
                {
                    //Clear our list
                    ddlTools.Items.Clear();
                    //Etc,,,
                }
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm going to bet you are looping endlessly really fast and running the stack out.

    In the method loadList(), line 7 calls a new loadList(), which is itself, which then calls a new loadList() which is itself, which calls a new loadList() which is itself... get the idea?

    Put a breakpoint at line 7 and step through it. I'll bet you see this getting called over and over.

    Comment

    • Meganutter
      New Member
      • Mar 2009
      • 47

      #3
      yes, i know that far.
      i also found out that d.invoke() will use the same thread
      so i must use this.invoke(d)
      *edit*
      capitals are different
      loadList
      LoadList

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Right. LoadList keeps making a new loadList. Over and over and over because it keeps calling itself in line 7.

        But it will only do this if the condition of line 5 is true.
        Try this: insert a line between 6 and 7 that makes ddtool.InvokeRe quired false. That way it will work the first time through, but be changed after it has done the first invoke.

        Comment

        • Meganutter
          New Member
          • Mar 2009
          • 47

          #5
          i already fixed it by using this.invoke(d)
          if i forced it to true i'd probably get a unsafe threadcall error since its still a different thread then the UI component (hence the eternal loop?)

          Comment

          • mldisibio
            Recognized Expert New Member
            • Sep 2008
            • 191

            #6
            In order to marshal the thread back to the UI thread, it is the control which needs to call invoke on the delegate. This is different than just creating a new delegate and calling invoke on it, because, as tlhintoq says, that just keeps calling itself, and since the thread has not been marshalled yet, the InvokeRequired condition remains true.

            Try the following:

            Code:
            delegate void LoadList();
            
            private void loadList()
                    {
                        if (ddlTools.InvokeRequired)
                        {
                            LoadList d = new LoadList(loadList);
                            ddlTools.Invoke(d);
                        }
                        else
                        {
                            //Clear our list
                            ddlTools.Items.Clear();
                            //Etc,,,
                        }
                    }

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              As long as the control used in the if statement is the same one used to .Invoke you should be fine.
              I use "this" for both and it seems to work well enough

              Comment

              Working...