Synchronized dataset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zürcher See

    #1

    Synchronized dataset

    I have in a form a datagrid that display a dataset, but if I start a new
    thread that fill the dataset the program will crash, that's because the
    dataset is not synchronized, is not thread safe.
    I tryed to use the monitor.enter(d ataset) and monitor.exit(da taset) befor
    updating the dataset but was useless.
    I think the problem is the paint method of the datagrid, that you can not
    override, so you can not synchronize the dataset.
    Also there is only one paint event, you need at least two, one befor and one
    after painting.

    Someone has a suggestion?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Synchronized dataset

    Zurcher,

    The problem is not that the data set is not synchronized, but when you
    bind the data set to a grid, then the grid sets up event handlers on the
    data set. When you change the data, the events fire, and the grid tries to
    update itself. If you want to do anything that affects the state of the UI,
    then that call must be made on the UI thread. What you need to do is
    marshal the call to the UI thread. In order to do this, create a method
    which will change your data. Then create a delegate which matches the
    signature of that method. Once you have that, on your thread that is
    performing the calcs, call the Invoke method on the grid, passing the
    delegate wrapping the method, and any parameters you need to pass.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Zürcher See" <aquila@cannabi smail.com> wrote in message
    news:1067518743 .233138@fuchs.c yberlink.ch...[color=blue]
    > I have in a form a datagrid that display a dataset, but if I start a new
    > thread that fill the dataset the program will crash, that's because the
    > dataset is not synchronized, is not thread safe.
    > I tryed to use the monitor.enter(d ataset) and monitor.exit(da taset) befor
    > updating the dataset but was useless.
    > I think the problem is the paint method of the datagrid, that you can not
    > override, so you can not synchronize the dataset.
    > Also there is only one paint event, you need at least two, one befor and[/color]
    one[color=blue]
    > after painting.
    >
    > Someone has a suggestion?
    >
    >[/color]


    Comment

    • Zürcher See

      #3
      Re: Synchronized dataset

      Thank's, I'll try.
      Can I ask you something else, there is a difference between this two way to
      start a new thread (to do not use that from the UI):

      System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
      System.Threadin g.WaitCallback( this.MyFunction ),null);

      or

      System.Threadin g.Thread tNewThread=new System.Threadin g.Thread(new
      System.Threadin g.ThreadStart(t his.MyFunction) );
      tNewThread.Star t();

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> schrieb
      im Newsbeitrag news:eyHx6ounDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Zurcher,
      >
      > The problem is not that the data set is not synchronized, but when you
      > bind the data set to a grid, then the grid sets up event handlers on the
      > data set. When you change the data, the events fire, and the grid tries[/color]
      to[color=blue]
      > update itself. If you want to do anything that affects the state of the[/color]
      UI,[color=blue]
      > then that call must be made on the UI thread. What you need to do is
      > marshal the call to the UI thread. In order to do this, create a method
      > which will change your data. Then create a delegate which matches the
      > signature of that method. Once you have that, on your thread that is
      > performing the calcs, call the Invoke method on the grid, passing the
      > delegate wrapping the method, and any parameters you need to pass.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Zürcher See" <aquila@cannabi smail.com> wrote in message
      > news:1067518743 .233138@fuchs.c yberlink.ch...[color=green]
      > > I have in a form a datagrid that display a dataset, but if I start a new
      > > thread that fill the dataset the program will crash, that's because the
      > > dataset is not synchronized, is not thread safe.
      > > I tryed to use the monitor.enter(d ataset) and monitor.exit(da taset)[/color][/color]
      befor[color=blue][color=green]
      > > updating the dataset but was useless.
      > > I think the problem is the paint method of the datagrid, that you can[/color][/color]
      not[color=blue][color=green]
      > > override, so you can not synchronize the dataset.
      > > Also there is only one paint event, you need at least two, one befor and[/color]
      > one[color=green]
      > > after painting.
      > >
      > > Someone has a suggestion?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Synchronized dataset

        Zurcher,

        Generally speaking, the ThreadPool should be used, as it eliminates the
        overhead of having to create a thread every time you want to perform a
        finite task. If you needed a thread which ran in a loop, over and over,
        then you would create a new instance of the Thread class. However, if you
        just want to perform a finite task asynchronously, then you would use the
        ThreadPool class.

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Zürcher See" <aquila@cannabi smail.com> wrote in message
        news:1067524855 .240649@fuchs.c yberlink.ch...[color=blue]
        > Thank's, I'll try.
        > Can I ask you something else, there is a difference between this two way[/color]
        to[color=blue]
        > start a new thread (to do not use that from the UI):
        >
        > System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
        > System.Threadin g.WaitCallback( this.MyFunction ),null);
        >
        > or
        >
        > System.Threadin g.Thread tNewThread=new System.Threadin g.Thread(new
        > System.Threadin g.ThreadStart(t his.MyFunction) );
        > tNewThread.Star t();
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> schrieb
        > im Newsbeitrag news:eyHx6ounDH A.1960@TK2MSFTN GP12.phx.gbl...[color=green]
        > > Zurcher,
        > >
        > > The problem is not that the data set is not synchronized, but when[/color][/color]
        you[color=blue][color=green]
        > > bind the data set to a grid, then the grid sets up event handlers on the
        > > data set. When you change the data, the events fire, and the grid tries[/color]
        > to[color=green]
        > > update itself. If you want to do anything that affects the state of the[/color]
        > UI,[color=green]
        > > then that call must be made on the UI thread. What you need to do is
        > > marshal the call to the UI thread. In order to do this, create a method
        > > which will change your data. Then create a delegate which matches the
        > > signature of that method. Once you have that, on your thread that is
        > > performing the calcs, call the Invoke method on the grid, passing the
        > > delegate wrapping the method, and any parameters you need to pass.
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - mvp@spam.guard. caspershouse.co m
        > >
        > > "Zürcher See" <aquila@cannabi smail.com> wrote in message
        > > news:1067518743 .233138@fuchs.c yberlink.ch...[color=darkred]
        > > > I have in a form a datagrid that display a dataset, but if I start a[/color][/color][/color]
        new[color=blue][color=green][color=darkred]
        > > > thread that fill the dataset the program will crash, that's because[/color][/color][/color]
        the[color=blue][color=green][color=darkred]
        > > > dataset is not synchronized, is not thread safe.
        > > > I tryed to use the monitor.enter(d ataset) and monitor.exit(da taset)[/color][/color]
        > befor[color=green][color=darkred]
        > > > updating the dataset but was useless.
        > > > I think the problem is the paint method of the datagrid, that you can[/color][/color]
        > not[color=green][color=darkred]
        > > > override, so you can not synchronize the dataset.
        > > > Also there is only one paint event, you need at least two, one befor[/color][/color][/color]
        and[color=blue][color=green]
        > > one[color=darkred]
        > > > after painting.
        > > >
        > > > Someone has a suggestion?
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Zürcher See

          #5
          Re: Synchronized dataset

          Thanks for everything

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> schrieb
          im Newsbeitrag news:ugoKmWvnDH A.2776@tk2msftn gp13.phx.gbl...[color=blue]
          > Zurcher,
          >
          > Generally speaking, the ThreadPool should be used, as it eliminates[/color]
          the[color=blue]
          > overhead of having to create a thread every time you want to perform a
          > finite task. If you needed a thread which ran in a loop, over and over,
          > then you would create a new instance of the Thread class. However, if you
          > just want to perform a finite task asynchronously, then you would use the
          > ThreadPool class.
          >
          > Hope this helps.
          >
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "Zürcher See" <aquila@cannabi smail.com> wrote in message
          > news:1067524855 .240649@fuchs.c yberlink.ch...[color=green]
          > > Thank's, I'll try.
          > > Can I ask you something else, there is a difference between this two way[/color]
          > to[color=green]
          > > start a new thread (to do not use that from the UI):
          > >
          > > System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
          > > System.Threadin g.WaitCallback( this.MyFunction ),null);
          > >
          > > or
          > >
          > > System.Threadin g.Thread tNewThread=new System.Threadin g.Thread(new
          > > System.Threadin g.ThreadStart(t his.MyFunction) );
          > > tNewThread.Star t();
          > >
          > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>[/color][/color]
          schrieb[color=blue][color=green]
          > > im Newsbeitrag news:eyHx6ounDH A.1960@TK2MSFTN GP12.phx.gbl...[color=darkred]
          > > > Zurcher,
          > > >
          > > > The problem is not that the data set is not synchronized, but when[/color][/color]
          > you[color=green][color=darkred]
          > > > bind the data set to a grid, then the grid sets up event handlers on[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > > > data set. When you change the data, the events fire, and the grid[/color][/color][/color]
          tries[color=blue][color=green]
          > > to[color=darkred]
          > > > update itself. If you want to do anything that affects the state of[/color][/color][/color]
          the[color=blue][color=green]
          > > UI,[color=darkred]
          > > > then that call must be made on the UI thread. What you need to do is
          > > > marshal the call to the UI thread. In order to do this, create a[/color][/color][/color]
          method[color=blue][color=green][color=darkred]
          > > > which will change your data. Then create a delegate which matches the
          > > > signature of that method. Once you have that, on your thread that is
          > > > performing the calcs, call the Invoke method on the grid, passing the
          > > > delegate wrapping the method, and any parameters you need to pass.
          > > >
          > > > Hope this helps.
          > > >
          > > >
          > > > --
          > > > - Nicholas Paldino [.NET/C# MVP]
          > > > - mvp@spam.guard. caspershouse.co m
          > > >
          > > > "Zürcher See" <aquila@cannabi smail.com> wrote in message
          > > > news:1067518743 .233138@fuchs.c yberlink.ch...
          > > > > I have in a form a datagrid that display a dataset, but if I start a[/color][/color]
          > new[color=green][color=darkred]
          > > > > thread that fill the dataset the program will crash, that's because[/color][/color]
          > the[color=green][color=darkred]
          > > > > dataset is not synchronized, is not thread safe.
          > > > > I tryed to use the monitor.enter(d ataset) and monitor.exit(da taset)[/color]
          > > befor[color=darkred]
          > > > > updating the dataset but was useless.
          > > > > I think the problem is the paint method of the datagrid, that you[/color][/color][/color]
          can[color=blue][color=green]
          > > not[color=darkred]
          > > > > override, so you can not synchronize the dataset.
          > > > > Also there is only one paint event, you need at least two, one befor[/color][/color]
          > and[color=green][color=darkred]
          > > > one
          > > > > after painting.
          > > > >
          > > > > Someone has a suggestion?
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...