1

I am trying to connect to a remote server to using Sharp SSH. The timeout exception I receive is a network based exception. I want the program to be more robust even if the network has problems, or if the server simply isn't available. The time out occurs at my open command and throws an IO timeout exception.

I am hoping to solve this intermittent problem from my end.

mySqlConnectionString = String.Format(
    "server={0};user={1};database=MTA;port=3306;password={2}", 
    SqlHost, mySqlUser, mySqlPass);

se = new SshExec(Host, User, Pass);
msc = new MySqlConnection(mySqlConnectionString);

// Connect to server and get the list of 
// firmware versions and serial numbers available
se.Connect();

lblConnected.Text = (se.Connected) ? "Connected" : "Not Connected";

msc.Open();         //exception happens here

My question: Is there some way I can tell the C# application to just try the connect command again should it encounter an exception?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Joe C
  • 184
  • 1
  • 15

1 Answers1

2

Try using a try catch block:

        mySqlConnectionString = String.Format("server={0};user={1};database=MTA;port=3306;password={2}", SqlHost, mySqlUser, mySqlPass);

        se = new SshExec(Host, User, Pass);
        msc = new MySqlConnection(mySqlConnectionString);
        // Connect to server and get the list of firmware versions and serial numbers available
        se.Connect();

        lblConnected.Text = (se.Connected) ? "Connected" : "Not Connected";

        try
        {
            msc.Open();  //exception happens here
        }
        catch (Exception)
        {
            //do stuff, i.e. retry connection
            msc.Open();
        }

to be sure you can use a nested try/catch:

        mySqlConnectionString = String.Format("server={0};user={1};database=MTA;port=3306;password={2}", SqlHost, mySqlUser, mySqlPass);

        se = new SshExec(Host, User, Pass);
        msc = new MySqlConnection(mySqlConnectionString);
        // Connect to server and get the list of firmware versions and serial numbers available
        se.Connect();

        lblConnected.Text = (se.Connected) ? "Connected" : "Not Connected";

        try
        {
            msc.Open();  //exception happens here
        }
        catch (Exception)
        {
            //do stuff, i.e. retry connection
            try
            {
                msc.Open();  //exception happens here
            }
            catch (Exception)
            {
                //do stuff
            }
        }
Andrea
  • 11,801
  • 17
  • 65
  • 72
  • That looks good. I can give that a try, no pun intended. Also there is a command that would route a program to a specific point X in the code if Y happens. I am not sure what it is called so I can't even look it up. Does anyone understand what I mean by that? – Joe C May 09 '13 at 12:22
  • Maybe you mean the [goto](http://msdn.microsoft.com/en-us/library/13940fs2%28v=vs.110%29.aspx) command, but I think you should just create a new method/function and call it – Andrea May 09 '13 at 13:24
  • Yeah GOTO probably won't do it either. If I can't find a better way, I'll just probably use a try catch scenario. What I'd prefer to do is adjust the MySqlConnection object to automatically reconnect or try again should the IO timeout. – Joe C May 09 '13 at 14:07