Bug in .NET Tab Control? Help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slylos

    Bug in .NET Tab Control? Help!

    I have an app written in C# (obviously). Here is my dilemma:

    The form loads, instantiates a bunch of worker threads that execute one at a
    time. When one finishes, it starts the next one in line. I have about 5
    datagrids on 5 different tab pages in a tab control. What I'm using the
    Threads for is pretty basic: get data from a database and fill a datagrid. I
    use delegates throughout to allow the worker thread to call the
    'FillDataGrid' method in the main thread (the form thread). I've had
    problems before with threads not accessing other threads controls which is
    why I use the delegates. But still it crashes with the following:

    An unhandled exception of type 'System.Argumen tException' occurred in
    system.windows. forms.dll

    Additional information: Controls created on one thread cannot be parented to
    a control on a different thread.

    I decided to add another tab page (out of pure imagination) as the first tab
    page in the control. voila! now everything loads properly (without changing
    any code whatsoever). Problem is, I try to programmaticall y remove the
    temporary page (with a generic "please wait while loading" message). It
    removes the page fine, but on the next page in line, all the controls
    dissappear. If I don't change or remove tab pages programmaticall y, all the
    controls stay intact. I'm not sure what I am missing, and leaving the temp
    page intact is just ugly. Anybody know what I'm missing?
  • Jon Skeet [C# MVP]

    #2
    Re: Bug in .NET Tab Control? Help!

    slylos <slylos@discuss ions.microsoft. com> wrote:[color=blue]
    > I have an app written in C# (obviously). Here is my dilemma:
    >
    > The form loads, instantiates a bunch of worker threads that execute one at a
    > time. When one finishes, it starts the next one in line. I have about 5
    > datagrids on 5 different tab pages in a tab control. What I'm using the
    > Threads for is pretty basic: get data from a database and fill a datagrid. I
    > use delegates throughout to allow the worker thread to call the
    > 'FillDataGrid' method in the main thread (the form thread). I've had
    > problems before with threads not accessing other threads controls which is
    > why I use the delegates. But still it crashes with the following:
    >
    > An unhandled exception of type 'System.Argumen tException' occurred in
    > system.windows. forms.dll
    >
    > Additional information: Controls created on one thread cannot be parented to
    > a control on a different thread.[/color]

    That suggests your threading isn't what you think it is. Are you sure
    you're using Control.Invoke rather than calling Invoke or BeginInvoke
    directly on the delegate?

    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • slylos

      #3
      Re: Bug in .NET Tab Control? Help!

      I'll try to give you more of my code without running out of space here:

      I've got a Windows form where the tabcontrol is located
      (frmScheduleReq uestsAdmin)
      I've got a db layer that connects to a web service that I built to retreive
      the records from the database (tsClientWebSer viceConnection. dll)
      the db layer has a method 'GetPendingSche duleRequests'. here is the
      delegates in the db layer:

      ------------------------------------>db layer
      code<----------------------------------------
      public delegate void PendingSchedule RequestsComplet e(DataSet dsPending);
      public static PendingSchedule RequestsComplet e PendingRequests Complete;

      //here is the method that retrieves the pending requests

      public void GetPendingSched uleRequests()
      {
      MyWebServiceObj ect obj = new MyWebServiceObj ect();//name changed =^)
      DataSet dsPending;

      try
      {
      dsPending = obj.GetPendingR equests();
      }
      catch(System.Ne t.WebException we)
      {
      //save error info to file
      }

      if (PendingRequest sComplete != null) //invoke delegate
      {
      PendingRequests Complete(dsPend ing);
      }
      }
      ------------------------------------>db layer
      code<----------------------------------------

      and here is the delegate connection and thread start
      ------------------------------------>Form
      code<------------------------------------------
      //form_load
      private void frmScheduleRequ estAdmin_Load(o bject sender, System.EventArg s e)
      {
      tsClientWebServ iceConnection ts = new tsClientWebServ iceConnection;
      tsClientWebServ iceConnection.P endingRequestsC omplete = new
      tsClientWebServ iceConnection.P endingScheduleR equestsComplete (LoadPendingSch eduleRequests);
      Thread tGetPending = new Thread(new
      ThreadStart(ts. GetPendingSched uleRequests));

      tGetPending.Sta rt();
      }


      //target for delegate
      private void LoadPendingSche duleRequests(Da taSet dsPending)
      {
      dsLocalPendingC opy = dsPending.Copy( );
      dgPending.DataS ource = dsLocalPendingC opy.Tables[0];
      ProgressComplet e();
      }
      //progress complete handles tab removal
      private void ProgressComplet e()
      {
      tcTabs.TabPages .Remove(tpLoadi ngPage);
      }

      ------------------------------------->Form
      code<-------------------------------------------

      when ProgressComplet e executes, thats when the next tab in line loses its
      controls ... please help!!

      [color=blue]
      > That suggests your threading isn't what you think it is. Are you sure
      > you're using Control.Invoke rather than calling Invoke or BeginInvoke
      > directly on the delegate?
      >
      > Could you post a short but complete program which demonstrates the
      > problem?
      >
      > See http://www.pobox.com/~skeet/csharp/complete.html for details of
      > what I mean by that.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too
      >[/color]

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Bug in .NET Tab Control? Help!

        slylos <slylos@discuss ions.microsoft. com> wrote:[color=blue]
        > I'll try to give you more of my code without running out of space here:
        >
        > I've got a Windows form where the tabcontrol is located
        > (frmScheduleReq uestsAdmin)
        > I've got a db layer that connects to a web service that I built to retreive
        > the records from the database (tsClientWebSer viceConnection. dll)
        > the db layer has a method 'GetPendingSche duleRequests'. here is the
        > delegates in the db layer:[/color]

        It looks like you're doing exactly what the error message is
        complaining about - updating a control from a non-UI thread.

        See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

        Just "using delegates" doesn't do everything automatically - you need
        to call Control.Invoke/BeginInvoke appropriately.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • slylos

          #5
          Re: Bug in .NET Tab Control? Help!

          Well I'm not having a problem with the error message; that went away as soon
          as I added a tabpage in front of the first tabpage with the datagrid. The
          problem I'm having is when I programmaticall y remove that first (temp)
          tabpage to expose the tabpage with the datagrid, all the controls on the tab
          with the datagrid disappear(inclu ding the datagrid, but not their tabpage
          parent).

          Like I said, I do the same exact thing in another form (with different data,
          same controls i.e. tab pages/datagrids) and I never have problems. The
          difference between that form and this form is the tabpage on that form is 3rd
          in the tabpage order, and on the new form, its the first tabpage in the
          order. The problem I'm having is controls mysteriously disappearing . . .

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Bug in .NET Tab Control? Help!

            slylos <slylos@discuss ions.microsoft. com> wrote:[color=blue]
            > Well I'm not having a problem with the error message; that went away
            > as soon as I added a tabpage in front of the first tabpage with the
            > datagrid. The problem I'm having is when I programmaticall y remove
            > that first (temp) tabpage to expose the tabpage with the datagrid,
            > all the controls on the tab with the datagrid disappear(inclu ding the
            > datagrid, but not their tabpage parent).[/color]

            Just adding a tabpage hasn't fixed your threading error though. I'm not
            sure why the error message went away, but the problem hasn't.
            [color=blue]
            > Like I said, I do the same exact thing in another form (with
            > different data, same controls i.e. tab pages/datagrids) and I never
            > have problems. The difference between that form and this form is the
            > tabpage on that form is 3rd in the tabpage order, and on the new
            > form, its the first tabpage in the order. The problem I'm having is
            > controls mysteriously disappearing . . .[/color]

            Just because something has worked in the past doesn't mean it was
            guaranteed to work, or that you weren't doing things wrong. If you
            access the UI from a different thread, you shouldn't be doing that.
            Your code appears to be doing that. Fix it (everywhere you're doing it,
            not just in the place where you're seeing problems), and then if it
            still doesn't work, we'll look again.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...