I have a Winforms application that does some hefty database work. It does a lot of reading and some inserting of records. The operation can take up to 30 seconds to a minute, depending on the size of the database. Right now, my program works just fine.
My problem is, while my program is doing this, my UI thread is being blocked till the database stuff is done. I’d like to put the database functions into a thread, however, according to this MSDN article, ADO.NET is not thread-safe.
Is there a way to provide a progress bar on my UI or some way around the “ADO.NET is not thread safe”? Maybe to write my own classes that inherit SqlDataReader or SqlConnection to make it thread-safe. Is this even possible?
Just use a backgroundworker and make sure each thread (bgw) creates its own Connection object.
And regarding to the linked article, make sure you have a Concurrency-safe way of generating keys. Letting the Db generate them is the best option.
That’s tricky. A Bgw can ReportProgress just fine but getting it from a running query is a problem. Even the total nr of rows is usually only known at the end.
But you can always fake it, the user doesn’t mind/know.