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?

1
  • can you not wrap it in a try catch and then try again if it fails Commented May 8, 2013 at 15:25

1 Answer 1

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
            }
        }
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Maybe you mean the goto command, but I think you should just create a new method/function and call it
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.