DataSet and thread

S

shark

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
 
J

Jon Skeet [C# MVP]

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
 
M

Marc Gravell

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
 
S

shark

I'm calling bindingsource.SuspenBinding 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.ResumeBinding ?
Data will be overriden ? or merged?

Thanks

SS
 
J

Jon Skeet [C# MVP]

shark said:
I'm calling bindingsource.SuspenBinding 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.ResumeBinding ?
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?
 
S

shark

OK, so
1. bindingsource.SuspenBinding
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.ResumeBinding

What will happen to my dataset ?

Thx

SS
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)


shark said:
OK, so
1. bindingsource.SuspenBinding
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.ResumeBinding

What will happen to my dataset ?

Thx

SS
 
S

shark

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

Thx

SS

Nicholas Paldino said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

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

Thx

SS

Nicholas Paldino said:
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]
- (e-mail address removed)


shark said:
OK, so
1. bindingsource.SuspenBinding
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.ResumeBinding

What will happen to my dataset ?

Thx

SS

I'm calling bindingsource.SuspenBinding 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.ResumeBinding ?
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top