Sharing Data between threads

C

Charlie Brown

I have an application that loops through a dataset and retrieves /
updates information in rows that meet certain criteria. When the end
user clicks a button, the dataset is then merged with the database. I
would like to make the merge process run in the background so that no
user interaction is required. I can make the background thread run
and update the dataset just fine. My questions are, can 2 threads
loop through a dataset at the same time? Can my background process
create a copy of the database and then merge the copy? Can my
background process download a new set of data and copy it over the
current dataset?

Any help is much appreciated.
 
G

Guest

My questions are, can 2 threads
loop through a dataset at the same time?

Yes, but you'll have to deal with concurrency issues. If two threads are
updating the data, which thread takes precendence? What if two threads
updating the same field at the same time?
Can my background process
create a copy of the database and then merge the copy?

How large is your database? What sort of updates are you applying?
Can my
background process download a new set of data and copy it over the
current dataset?

Yes, but you'll need to deal with concurrency issues.
 
C

Charlie Brown

Yes, but you'll have to deal with concurrency issues. If two threads are
updating the data, which thread takes precendence? What if two threads
updating the same field at the same time?


How large is your database? What sort of updates are you applying?


Yes, but you'll need to deal with concurrency issues.

Thanks for the reply. The dataset is small, say around 2000 rows, 6
columns, max. The UI searches for a specific row every few seconds
and updates a boolean value. The background task loops through the
dataset at a set interval and updates the database by calling
SqlCommand.ExecuteNonQuery on the rows that have been affected. Once
all affected rows have been updated, it creates a new dataset and then
copies over the original.

The only problem with this approach would be if the UI updates the
dataset after the merge completes, but before the new data is copied
down from the database. Basically I would lose that data.
 
G

Guest

Thanks for the reply. The dataset is small, say around 2000 rows, 6
columns, max. The UI searches for a specific row every few seconds
and updates a boolean value. The background task loops through the
dataset at a set interval and updates the database by calling
SqlCommand.ExecuteNonQuery on the rows that have been affected. Once
all affected rows have been updated, it creates a new dataset and then
copies over the original.

Datasets provide events for changed rows/columns/cells. Perhaps you can
hook into these events to act on the changes rather than polling on the
dataset?
The only problem with this approach would be if the UI updates the
dataset after the merge completes, but before the new data is copied
down from the database. Basically I would lose that data.

You'll need to do some synclocking to prevent concurrency issues.
 

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