Mysql database freezes with Visual Sudio C#
-
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; } }
-
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; } }
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.
-
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; } }
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 -
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; } }
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