Hi.
My program hangs when I try to open a connection to a non-existing server ( when my server is not running ).
Of course it's not acceptable that my program won't continue, when a connection can't be made.
When I put the Open() method in a try block and catch MySql.Data.MySqlClient.MySqlException then it won't hang and I get the Message ( .Message of the exception ): Unable to connect to any of the specified MySQL hosts after about a second ( my timeout is set to 5, though changing it doesn't affect the time that it takes to get the error ).
When I am using the "using"-statement like this:
using(var connection = new MySqlConnection(myConnectionString))
{
connection.Open();
}
Then it won't work ( program hangs ).
Another problem is with the MySqlDataReader.
It will also hang if there is no connection.
If I catch Exception ( not MySqlException ) I got it working at some point, but again this also won't work with the "using" statement.
The MySQL is usually running but as I said before, I can't accept my program to not work properly when no connection could be made.
Why can't I use the using-statement and what's the best way to handle it, when a connection can't be made?
My program hangs when I try to open a connection to a non-existing server ( when my server is not running ).
Of course it's not acceptable that my program won't continue, when a connection can't be made.
When I put the Open() method in a try block and catch MySql.Data.MySqlClient.MySqlException then it won't hang and I get the Message ( .Message of the exception ): Unable to connect to any of the specified MySQL hosts after about a second ( my timeout is set to 5, though changing it doesn't affect the time that it takes to get the error ).
When I am using the "using"-statement like this:
using(var connection = new MySqlConnection(myConnectionString))
{
connection.Open();
}
Then it won't work ( program hangs ).
Another problem is with the MySqlDataReader.
It will also hang if there is no connection.
If I catch Exception ( not MySqlException ) I got it working at some point, but again this also won't work with the "using" statement.
The MySQL is usually running but as I said before, I can't accept my program to not work properly when no connection could be made.
Why can't I use the using-statement and what's the best way to handle it, when a connection can't be made?