private void DatabaseConnectionMonitorThread()
{
int Timeout = 1000;
for (;;)
{
uint StartTick;
try
{
Process TunnelProcess;
string CmdLine = GetTunnelProcessCmdLine();
TunnelProcess = CreateProcessInJob(PlinkFilePath, CmdLine, JobObject);
try
{
TunnelProcess.WaitForInputIdle(20000);
}
catch (System.InvalidOperationException)
{
//
// Process wasn't a GUI app, don't bother waiting.
//
}
catch
{
try
{
TunnelProcess.Kill();
}
catch
{
}
throw;
}
InitCompletedEvent.Set();
StartTick = (uint)Environment.TickCount;
TunnelProcess.WaitForExit();
//
// Clear the reconnect timeout to the minimum value if the
// process appeared to start successfully and stay online
// for more than 30 seconds.
//
if ((uint)Environment.TickCount - StartTick > 30000)
{
Timeout = 1000;
}
else
{
if (Timeout < 32000)
{
Timeout *= 2;
}
}
}
catch (Exception e)
{
Logger.Log("DatabaseConnector.DatabaseConnectionMonitorThread: Exception managing database secure tunnel: {0}", e);
if (Timeout < 32000)
{
Timeout *= 2;
}
}
//
// Wait for a truncated expontential backoff before trying
// again.
//
Logger.Log("DatabaseConnector.DatabaseConnectionMonitorThread: Database secure tunnel closed, attempting reconnect in {0} milliseconds.", Timeout);
Thread.Sleep(Timeout);
}
}