As part of a large automation process, we call a third-party API that does some work that calls services on another machine. Recently, we found that every time another machine is unavailable, the API call sometimes hangs up to 40 minutes when trying to connect to a remote server.
The API that we use does not offer a way to specify a timeout, and we do not want our program to wait so long, so I thought that threads would be a good way to provide a timeout. The resulting code looks something like this:
Thread _thread = new Thread(_caller.CallServices());
_thread.Start();
_thread.Join(timeout);
if (_thread.IsAlive)
{
_thread.Abort();
throw new Exception("Timed-out attempting to connect.");
}
Basically, I want APICall () to start, but if it still goes after the timeout expires, suppose it fails, kill it and continue.
# .NET. , :
/ .net , , - gotchas ?