DataSet and thread

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

    #1

    DataSet and thread

    Hi.

    I have DataGridView bound to my dataset through bindingsource component. My
    dataset is filled on separate thread. While this thread is working I receive
    "Cross-thread operation not valid" exception because bindingsource tries to
    update grid cells from another thread (invoke is required).
    How to handle such scenario ?

    Thanks

    SS

  • Jon Skeet [C# MVP]

    #2
    Re: DataSet and thread

    On Dec 19, 10:37 am, "shark" <m...@poczta.on et.plwrote:
    I have DataGridView bound to my dataset through bindingsource component. My
    dataset is filled on separate thread. While this thread is working I receive
    "Cross-thread operation not valid" exception because bindingsource tries to
    update grid cells from another thread (invoke is required).
    How to handle such scenario ?
    You either need to temporarily unbind the data source while it's being
    filled, or fill to a "temporary" dataset that you then copy over on
    the UI thread. (At least, that's my understanding of the situation. I
    agree it's not terribly pleasant.)

    Jon

    Comment

    • Marc Gravell

      #3
      Re: DataSet and thread

      Grid bindings are unfortunately very thread-sensitive. For simple
      bindings (to individual properties) I have a workaround - but for this
      scenario I'm afraid you're either going to have to do manual binding,
      or suspend the binding while you populate the dataset on the worker
      thread and reinstate once ready.

      Marc

      Comment

      • shark

        #4
        Re: DataSet and thread

        I'm calling bindingsource.S uspenBinding just before Thread is started. It
        works fine, but my question is what will hapen to data is user change it
        manually and then I call bindingsource.R esumeBinding ?
        Data will be overriden ? or merged?

        Thanks

        SS

        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:ae1275c4-4810-4d98-b895-fd8a6ac791f2@e4 g2000hsg.google groups.com...
        On Dec 19, 10:37 am, "shark" <m...@poczta.on et.plwrote:
        >I have DataGridView bound to my dataset through bindingsource component.
        >My
        >dataset is filled on separate thread. While this thread is working I
        >receive
        >"Cross-thread operation not valid" exception because bindingsource tries
        >to
        >update grid cells from another thread (invoke is required).
        >How to handle such scenario ?
        >
        You either need to temporarily unbind the data source while it's being
        filled, or fill to a "temporary" dataset that you then copy over on
        the UI thread. (At least, that's my understanding of the situation. I
        agree it's not terribly pleasant.)
        >
        Jon

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: DataSet and thread

          shark <mark@poczta.on et.plwrote:
          I'm calling bindingsource.S uspenBinding just before Thread is started. It
          works fine, but my question is what will hapen to data is user change it
          manually and then I call bindingsource.R esumeBinding ?
          Data will be overriden ? or merged?
          It's not quite clear what sequence of events you're worried about.
          Could you spell it out in more detail?

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          World class .NET training in the UK: http://iterativetraining.co.uk

          Comment

          • shark

            #6
            Re: DataSet and thread

            OK, so
            1. bindingsource.S uspenBinding
            2. Thread is started
            3. Thread is pulling data to dataset
            4. User is working with DataGridView (which is unbound from dataset) and is
            changing data in it
            5. Thread stops
            6. bindingsource.R esumeBinding

            What will happen to my dataset ?

            Thx

            SS

            "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
            news:MPG.21d322 658f94cb3f71e@m snews.microsoft .com...
            shark <mark@poczta.on et.plwrote:
            >I'm calling bindingsource.S uspenBinding just before Thread is started. It
            >works fine, but my question is what will hapen to data is user change it
            >manually and then I call bindingsource.R esumeBinding ?
            >Data will be overriden ? or merged?
            >
            It's not quite clear what sequence of events you're worried about.
            Could you spell it out in more detail?
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            World class .NET training in the UK: http://iterativetraining.co.uk

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: DataSet and thread

              SS,

              This scenario shouldn't be allowed to happen. You should be disabling
              access to the data grid view while the thread is updating the underlying
              data that the user is seeing in the grid.

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


              "shark" <mark@poczta.on et.plwrote in message
              news:fkb4fo$hh0 $1@news.onet.pl ...
              OK, so
              1. bindingsource.S uspenBinding
              2. Thread is started
              3. Thread is pulling data to dataset
              4. User is working with DataGridView (which is unbound from dataset) and
              is changing data in it
              5. Thread stops
              6. bindingsource.R esumeBinding
              >
              What will happen to my dataset ?
              >
              Thx
              >
              SS
              >
              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:MPG.21d322 658f94cb3f71e@m snews.microsoft .com...
              >shark <mark@poczta.on et.plwrote:
              >>I'm calling bindingsource.S uspenBinding just before Thread is started.
              >>It
              >>works fine, but my question is what will hapen to data is user change it
              >>manually and then I call bindingsource.R esumeBinding ?
              >>Data will be overriden ? or merged?
              >>
              >It's not quite clear what sequence of events you're worried about.
              >Could you spell it out in more detail?
              >>
              >--
              >Jon Skeet - <skeet@pobox.co m>
              >http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              >World class .NET training in the UK: http://iterativetraining.co.uk
              >

              Comment

              • shark

                #8
                Re: DataSet and thread

                I'm going to do this (disable grid) but I just wonder what could happpen

                Thx

                SS

                "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
                message news:ONRAWXlQIH A.4656@TK2MSFTN GP03.phx.gbl...
                SS,
                >
                This scenario shouldn't be allowed to happen. You should be disabling
                access to the data grid view while the thread is updating the underlying
                data that the user is seeing in the grid.
                >
                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m
                >
                >
                "shark" <mark@poczta.on et.plwrote in message
                news:fkb4fo$hh0 $1@news.onet.pl ...
                >OK, so
                >1. bindingsource.S uspenBinding
                >2. Thread is started
                >3. Thread is pulling data to dataset
                >4. User is working with DataGridView (which is unbound from dataset) and
                >is changing data in it
                >5. Thread stops
                >6. bindingsource.R esumeBinding
                >>
                >What will happen to my dataset ?
                >>
                >Thx
                >>
                >SS
                >>
                >"Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                >news:MPG.21d32 2658f94cb3f71e@ msnews.microsof t.com...
                >>shark <mark@poczta.on et.plwrote:
                >>>I'm calling bindingsource.S uspenBinding just before Thread is started.
                >>>It
                >>>works fine, but my question is what will hapen to data is user change
                >>>it
                >>>manually and then I call bindingsource.R esumeBinding ?
                >>>Data will be overriden ? or merged?
                >>>
                >>It's not quite clear what sequence of events you're worried about.
                >>Could you spell it out in more detail?
                >>>
                >>--
                >>Jon Skeet - <skeet@pobox.co m>
                >>http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                >>World class .NET training in the UK: http://iterativetraining.co.uk
                >>
                >
                >

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: DataSet and thread

                  It really depends on what the other thread is doing, and what the user
                  is going to do. If what they are updating are separate, disparate parts of
                  the data, then nothing would happen. However, whether or not it is
                  disparate is something that is specific to your app and your data.

                  Regardless, it's not something I would consider allowing, unless you
                  have a severe need for it (in which case, you will have to find out how to
                  do it the right way, with proper synchronization ).

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

                  "shark" <mark@poczta.on et.plwrote in message
                  news:fkbesa$kbo $1@news.onet.pl ...
                  I'm going to do this (disable grid) but I just wonder what could happpen
                  >
                  Thx
                  >
                  SS
                  >
                  "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
                  in message news:ONRAWXlQIH A.4656@TK2MSFTN GP03.phx.gbl...
                  >SS,
                  >>
                  > This scenario shouldn't be allowed to happen. You should be disabling
                  >access to the data grid view while the thread is updating the underlying
                  >data that the user is seeing in the grid.
                  >>
                  >--
                  > - Nicholas Paldino [.NET/C# MVP]
                  > - mvp@spam.guard. caspershouse.co m
                  >>
                  >>
                  >"shark" <mark@poczta.on et.plwrote in message
                  >news:fkb4fo$hh 0$1@news.onet.p l...
                  >>OK, so
                  >>1. bindingsource.S uspenBinding
                  >>2. Thread is started
                  >>3. Thread is pulling data to dataset
                  >>4. User is working with DataGridView (which is unbound from dataset) and
                  >>is changing data in it
                  >>5. Thread stops
                  >>6. bindingsource.R esumeBinding
                  >>>
                  >>What will happen to my dataset ?
                  >>>
                  >>Thx
                  >>>
                  >>SS
                  >>>
                  >>"Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  >>news:MPG.21d3 22658f94cb3f71e @msnews.microso ft.com...
                  >>>shark <mark@poczta.on et.plwrote:
                  >>>>I'm calling bindingsource.S uspenBinding just before Thread is started.
                  >>>>It
                  >>>>works fine, but my question is what will hapen to data is user change
                  >>>>it
                  >>>>manually and then I call bindingsource.R esumeBinding ?
                  >>>>Data will be overriden ? or merged?
                  >>>>
                  >>>It's not quite clear what sequence of events you're worried about.
                  >>>Could you spell it out in more detail?
                  >>>>
                  >>>--
                  >>>Jon Skeet - <skeet@pobox.co m>
                  >>>http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                  >>>World class .NET training in the UK: http://iterativetraining.co.uk
                  >>>
                  >>
                  >>
                  >

                  Comment

                  Working...