Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mysql database freezes with Visual Sudio C#

Mysql database freezes with Visual Sudio C#

Scheduled Pinned Locked Moved C#
csharpdatabasemysqlvisual-studiosysadmin
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 14055879
    wrote on last edited by
    #1

    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?

    Her 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;
            }
        }
    
    R D R 3 Replies Last reply
    0
    • M Member 14055879

      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?

      Her 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;
              }
          }
      
      R Offline
      R Offline
      RedDk
      wrote on last edited by
      #2

      Having used neither raspberry pi and/or mysql EVER, I might be chasing down a pot-o-fool's gold here (Raspberry Pi? That's expired pharmacy my friend) but I see THAT connString and always say to myself "config issue/networking port" issue. I might open up Task Manager and set the display to anything that resembles the realtime CPU use, probably where there's a network monitoring graph that one can set the sample speed FASTER, then go through the motions which cause this "freeze" ... and see if the two things align in time. Come to think of it, VS (C#? Unlikely slow (blazes fast eh?)) ... leaves frozen footprints sometimes when running debug. Now, there's an idea! Run debug while under VS gravitational pull.

      1 Reply Last reply
      0
      • M Member 14055879

        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?

        Her 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;
                }
            }
        
        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        That "freeze" is the connection object giving the server time to respond to the connection request. When that times out, that's when you get the exception message. What do you do about that? Set the Connect Timeout[^] in the connection string.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • M Member 14055879

          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?

          Her 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;
                  }
              }
          
          R Online
          R Online
          Richard Deeming
          wrote on last edited by
          #4

          Assuming the MySql connector supports it, you could try using an async method:

          private void FormMeasurement_Load(object sender, EventArgs e)
          {
          listBoxMeasurement.Enabled = false;
          _ = LoadFormAsync();
          }

          private async Task LoadFormAsync()
          {
          try
          {
          using (var connection = new MySqlConnection(connString))
          {
          await connection.OpenAsync();

                  const string query = "select \* from weather";
                  using (var command = new MySqlCommand(query, connection))
                  using (var reader = await command.ExecuteReaderAsync())
                  {
                      while (await reader.ReadAsync())
                      {
                          listBoxMeasurement.Items.Add(string.Format("Temperature: {0}   Humidity: {1}   Date: {2}", 
                              reader.GetString("temp"), reader.GetString("hum"), reader.GetString("datecreated")));
                      }
                  }
              }
              
              listBoxMeasurement.Enabled = true;
          }
          catch (Exception ex)
          {
              MessageBox.Show("Database is not available for moment!\\n" + ex.Message);
          }
          

          }


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups