0

I have a BackgroundWorker that I use on a Search. Two different Searches can be done, one to a SQL DB and another to an Accpac DB. To accommodate for slow execution time to the Accpac DB, I use Thread.Sleep. The problem is, when I do a search on my SQL DB, I give parameters stating it should only return active records. If I move the Thread.Sleep to accommodate for my Accpac search, then my SQL results return all records instead of Active only. If I move Thread.Sleep to accommodate for my SQL search, then there are issues on the Accpac search results returned.

Are there any other alternatives other than Thread.Sleep?

Sam
  • 1,509
  • 3
  • 19
  • 28
Melanie
  • 584
  • 2
  • 11
  • 31
  • 4
    It's not clear why you're using Thread.Sleep in the first place. You talk about "accommodating" slow execution time, but it's not at all clear what you mean by that. Why aren't you launching the two queries in parallel and just waiting for them both to finish? Also, please indicate the version of .NET you're using, as .NET 4 may well have a neater answer to this than earlier versions. – Jon Skeet Aug 30 '11 at 10:13
  • .NET 4. We decided to use Thread.Sleep by chance, while debugging it seemed to fix the problem at that time. But seems it has caused more unforeseen problems than really do any good. – Melanie Aug 30 '11 at 10:15
  • You still haven't told us what the problem was... – Jon Skeet Aug 30 '11 at 10:16
  • I have a BusyInidicator on a custom Search screen. The problem was that, when the Search made a call to Accpac, the BusyIndicator didn't fire and the User also had to click on search twice, then wait (without BusyIndicator) for results. Thread.Sleep was added and this seemed to fix the problem. The BusyIndicator showed fine and the User only needed to click Search once. – Melanie Aug 30 '11 at 10:20
  • Sounds like you never worked out *why* it wasn't working before. (I don't know what a BusyIndicator is, I'm afraid, but adding Thread.Sleep is almost *never* the right solution.) – Jon Skeet Aug 30 '11 at 10:23

1 Answers1

1

Sounds like you should look into Thread Synchronization classes such as WaitHandles. A ManualResetEvent would probably work pretty well for you.
http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185