this is my question:
How could I handle a timeout on a method execution, the scenario is the following:
I have a C# .Net framework 4.0 Windows Service application. I added a reference to a external COM assembly (of a specific software of certain company). With this assembly I intented to process some information against that third party software on my Windows Service, and repeat that task every X minutes (defined by user). This works well on a perfect scenario, the issue is when that third party software has some problems, that assembly just don't response and my Windows service get stuck on that execution with no answer, for example this is the code of that execution:
var calObj = new CAL.Object(CalOperatins.SendNoSavingChanges);
//this SYNC execution method sometimes get stuck and
//the rest of the program just doesn't executes
calObj.TerminateUserSession();
log.Log4Text(string.Format("Service cycle ended, waiting {0} for next execution.",paramTime));
}
As I mentioned on the comments of the code, the execution of TerminateUserSession is synchronous and if that software experiments some problem, my service get stucked in there and the Log4Text method (and everything below it) does not executes.
Is there a way that I can handle a timeout for that TerminateUserSession method execution, keeping in mind that it's an external assembly and I can't change that code? (yes, I can figure out that it's wrong programmed because of the lack of a timeout)
Thanks in advance.
What about running that code on a thread for which you have a handle. You can track the successful execution by toggling a boolean back and forth and kill the thread if it doesn't respond by the specified timeout. After killing the thread you could relaunch it. However, this is not a perfect solution you might have to recreate your COM object when you kill the thread which always seems to take an excessive amount of time.
This would also be subject to what is happening in the COM object and if the TerminateUserSessions method can handle the effect.