Mysql database freezes with Visual Sudio C#.

Ken Ekholm 211 Reputation points
2022-12-18T20:32:57.57+00:00

I a'm reading values from raspberry pi mysql database with visual studio c#
If the database is not available and I am trying to read from it, the c# app
freezes for about 15 seconds.

Is it possible to make it not to freeze when checking if the database is available?

Here is the code

 string connString = "SERVER='192.168.86.41';DATABASE='spaceinformation';UID='******';PASSWORD='******'";  
 private void FormMeasurement_Load(object sender, EventArgs e)  
        {  
            try  
            {  
                using (var connection = new MySqlConnection(connString))  
                {  
                    connection.Open();  
  
                    query = "select * from weather";  
                    using (var command = new MySqlCommand(query, connection))  
                    {  
                        using (var reader = command.ExecuteReader())  
                        {  
                            while (reader.Read())  
                            {  
                                listBoxMeasurement.Items.Add(string.Format("Temperature: {0}   Humidity: {1}   Date: {2}", reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));  
                            }  
                        }  
                    }  
                    connection.Close();  
                }  
            }  
            catch  
            {  
                MessageBox.Show("Database is not available for moment!");  
                listBoxMeasurement.Enabled = false;  
            }  
        }  
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2022-12-18T21:11:03.46+00:00

    I believe this is because the MySqlConnection.Open is a blocking IO operation & it's waiting for the default timeout to elapse before reaching your catch block.

    There is an OpenAsync method that is the asynchronous non-blocking version of this method, but unfortunately the MySql driver for C# hasn't actually implemented it, so it just falls back to the Open implementation. It states that in their docs too:
    https://dev.mysql.com/doc/dev/connector-net/6.10/html/Methods_T_MySql_Data_MySqlClient_MySqlConnection.htm

       The cancellation token can optionally be honored.The default implementation invokes the synchronous Open() call and returns a completed task  
    

    You can work around that by running your MySql block inside a new Task, which won't prevent your FormMeasurement_Load from finishing. You can do that like this:

       _ = Task.Run(async () => {  
           ...  
       });  
    

    The code inside that callback would be your existing code.

    EDIT:
    You'll also just need to replace this:

       listBoxMeasurement.Items.Add(string.Format("Temperature: {0}   Humidity: {1}   Date: {2}", reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));  
    

    For this:

       listBoxMeasurement.BeginInvoke(() => {  
           listBoxMeasurement.Items.Add(string.Format("Temperature: {0}   Humidity: {1}   Date: {2}", reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));  
       });  
    

    And this:

       listBoxMeasurement.Enabled = false;  
    

    For this:

       listBoxMeasurement.BeginInvoke(() => {  
           listBoxMeasurement.Enabled = false;  
       });  
    
    1 person found this answer helpful.
    0 comments No comments

  2. nency jen 1 Reputation point
    2022-12-29T10:11:05.327+00:00

    may be your problem will be solved after writing a parameter.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.