Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application that is doing reads from an OPC Server at a rate of one every tenth of a second with each read being in its own thread. It then creates a thread to write that information into the database.

Overall this seems to work fine but when I stop the process and try and restart it again a short time later, I get a lot of errors trying to write to the database which seem to suggest that the SqlReader is busy and hasn't been closed.

So I'm wondering what the solution options might be?

One thought I had was to create a thread that sat and slept until it was triggered to wake and write information to the database and then go back to sleep. This would seem to be a good option but would require that I understand how to actually trigger threads to wake up as well as handling the shutting down of the thread when the application is trying to close (which I currently have never done).

Part of me is wondering if I need to be putting monitor controls around my SQL connection to ensure that it closes the read before releasing the variable to other processes ... that perhaps that is the source of my problems.

I'm not sure what other options there might be so I would appreciate any thoughts or comments.

Thanks in advance.
Posted

1 solution

You can only have one SqlReader per SqlConnection, try creating connections for each thread separately.
 
Share this answer
 
Comments
BHort 3-Oct-11 12:10pm    
But wouldn't that create a lot of overhead creating a new connection to the database for each thread?
Mehdi Gholam 3-Oct-11 12:20pm    
Not really, and it's either that or the overruns you are getting. :)
BHort 3-Oct-11 12:34pm    
Implemented the change plus added the command to reduce the priority of my DB save routine thread from RealTime to Normal priority (I previously had the entire app at Real Time priority) ... my application is keeping to within 3/1000ths of a second accuracy ... oh am I happy!

Thank you. Things are much more stable now.
Mehdi Gholam 3-Oct-11 12:35pm    
Good job!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900