I have a rather long-running process in a Windows Service that periodically throws a "ContextSwitchDeadlock" exception:
I have also rigged my service to send myself emails with details about encountered exceptions. I get:
Date: 05/25/2016 09:16:32:
Exception message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Exception Source: .Net SqlClient Data Provider
...and then three seconds later this:
Date: 05/25/2016 09:16:35:
Exception message: Cannot find table 0.
Exception Source: System.Data
BTW: At least it's consistent: I have had three pairs of those exceptions, and each time the second one is datetimed exactly three seconds after the first.
I have had prior experience with jimmying the SQLCommand's CommandTimeout value (currently set at 360) this way or that, and it seems sort of like throwing darts blindfolded or even black magic. Is there a better way to prevent such deadlocks?
I have other very similar methods that don't cause this problem; those take less time. The duration of the method (specifically, the length of time it takes the query to run) seems to be "the rub." I can't change that - "it is what it is" - so what else can I do?
UPDATE
Naturally, the recalcitrant process "made a liar out of me" right after posting the above, as I got two more pairs of the exceptions, this time with the second err msg of the pair occurring one second after the first instead of three. If I wait long enough, maybe the second exception will occur before the first one.