ACR_DatabaseConnector.DatabaseConnector.DatabaseConnectionMonitorThread C# (CSharp) Method

DatabaseConnectionMonitorThread() private method

The thread procedure for the thread that monitors the PLINK.EXE instance that provides a secure SSH port forward to the MySQL database. Initially, it creates a PLINK.EXE process, and from that point it ensures that if the PLINK.EXE process closes, that a new process is started to connect out to the database automatically in the event of a temporary connectivity interruption.
private DatabaseConnectionMonitorThread ( ) : void
return void
        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);
            }
        }