Thread problem with control delegate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jecheney@gmail.com

    Thread problem with control delegate

    Hi,
    >From the code below, I have this method/delegate which is called from
    another thread, Debug_Console is a listbox control on my main form, i
    created the delegate to avoid the cross threading error. Now the
    strange thing is, when i call the method from my other thread, the
    entry string is added to the listbox, but it also throws an exception
    after the string is added. I cant work out why.

    The exception is:
    =============== ==========
    System.InvalidO perationExcepti on: Cross-thread operation not valid:
    Control 'DebugConsole' accessed from a thread other than the thread it
    was created on.
    at System.Windows. Forms.Control.g et_Handle()
    at System.Windows. Forms.Control.S endMessage(Int3 2 msg, Int32
    wparam, String lparam)
    at System.Windows. Forms.ListBox.N ativeAdd(Object item)
    at System.Windows. Forms.ListBox.O bjectCollection .AddInternal(Ob ject
    item)
    at System.Windows. Forms.ListBox.O bjectCollection .Add(Object item)
    at UsenetProject.M ainForm.Debug_C onsole_Add_Item (String entry) in C:
    \Users\Jack\Doc uments\Misc Stuff\My Programs\Usenet Project
    \UsenetProject\ UsenetProject\F orm1.cs:line 46
    =============== ==========

    I thought the whole point of invoking the new delgate is to avoid the
    cross threading problem, what am i doing wrong?

    My code:
    public delegate void UpdateDebugCons oleDelegate(str ing entry);

    public void Debug_Console_A dd_Item(string entry)
    {
    if (this.DebugCons ole.InvokeRequi red)
    {
    UpdateDebugCons oleDelegate theDelegate = new
    UpdateDebugCons oleDelegate(thi s.Debug_Console _Add_Item);
    this.Invoke(the Delegate, new object[] { entry });
    }

    try
    {
    DebugConsole.It ems.Add(entry);
    }
    catch(Exception e)
    {
    MessageBox.Show (e.ToString());
    }
    }


    Thanks very much for any help,
    Jack

  • Peter Duniho

    #2
    Re: Thread problem with control delegate

    On Wed, 04 Apr 2007 15:46:05 -0700, <jecheney@gmail .comwrote:
    [...]
    I thought the whole point of invoking the new delgate is to avoid the
    cross threading problem, what am i doing wrong?
    Looks to me like you left out an "else". :)

    Your method should only do the real work in the
    !this.DebugCons ole.InvokeRequi red case. Put the whole try/catch block
    where you do the real work in the else clause of your if statement and
    everything should be fine.

    Pete

    Comment

    • jecheney@gmail.com

      #3
      Re: Thread problem with control delegate

      On 4 Apr, 23:46, jeche...@gmail. com wrote:
      Hi,
      >
      From the code below, I have this method/delegate which is called from
      >
      another thread, Debug_Console is a listbox control on my main form, i
      created the delegate to avoid the cross threading error. Now the
      strange thing is, when i call the method from my other thread, the
      entry string is added to the listbox, but it also throws an exception
      after the string is added. I cant work out why.
      >
      The exception is:
      =============== ==========
      System.InvalidO perationExcepti on: Cross-thread operation not valid:
      Control 'DebugConsole' accessed from a thread other than the thread it
      was created on.
      at System.Windows. Forms.Control.g et_Handle()
      at System.Windows. Forms.Control.S endMessage(Int3 2 msg, Int32
      wparam, String lparam)
      at System.Windows. Forms.ListBox.N ativeAdd(Object item)
      at System.Windows. Forms.ListBox.O bjectCollection .AddInternal(Ob ject
      item)
      at System.Windows. Forms.ListBox.O bjectCollection .Add(Object item)
      at UsenetProject.M ainForm.Debug_C onsole_Add_Item (String entry) in C:
      \Users\Jack\Doc uments\Misc Stuff\My Programs\Usenet Project
      \UsenetProject\ UsenetProject\F orm1.cs:line 46
      =============== ==========
      >
      I thought the whole point of invoking the new delgate is to avoid the
      cross threading problem, what am i doing wrong?
      >
      My code:
      public delegate void UpdateDebugCons oleDelegate(str ing entry);
      >
      public void Debug_Console_A dd_Item(string entry)
      {
      if (this.DebugCons ole.InvokeRequi red)
      {
      UpdateDebugCons oleDelegate theDelegate = new
      UpdateDebugCons oleDelegate(thi s.Debug_Console _Add_Item);
      this.Invoke(the Delegate, new object[] { entry });
      }
      >
      try
      {
      DebugConsole.It ems.Add(entry);
      }
      catch(Exception e)
      {
      MessageBox.Show (e.ToString());
      }
      }
      >
      Thanks very much for any help,
      Jack
      Brilliant thanks very much, that fixed it.

      Comment

      • jecheney@gmail.com

        #4
        Re: Thread problem with control delegate

        Given the above (working) code, i now have the problem that if i close
        the main form/program while the new thread is working, I get an
        exception that it cannot access a disposed object.

        what's the best way to avoid this problem? i.e stopping the thread
        before the main form object is disposed.




        Comment

        • gshzheng

          #5
          Re: Thread problem with control delegate

          this.DebugConso le.InvokeRequir ed

          should be

          this.InvokeRequ ired

          <jecheney@gmail .com>
          ??????:11757290 42.134981.26625 0@n76g2000hsh.g ooglegroups.com ...
          Given the above (working) code, i now have the problem that if i close
          the main form/program while the new thread is working, I get an
          exception that it cannot access a disposed object.
          >
          what's the best way to avoid this problem? i.e stopping the thread
          before the main form object is disposed.
          >
          >
          >
          >
          >

          Comment

          • Peter Duniho

            #6
            Re: Thread problem with control delegate

            On Wed, 04 Apr 2007 16:24:02 -0700, <jecheney@gmail .comwrote:
            Given the above (working) code, i now have the problem that if i close
            the main form/program while the new thread is working, I get an
            exception that it cannot access a disposed object.
            >
            what's the best way to avoid this problem? i.e stopping the thread
            before the main form object is disposed.
            Hard to say for sure. It depends on what exactly your form and your
            thread do and are doing when the form is closed. However, the two usual
            approaches would be to either alert the thread to stop processing and exit
            before you close the form, or to have the thread check to see if the form
            is still around before trying to use it (in the invoking method, for
            example).

            Pete

            Comment

            Working...