Serena
Serena

Reputation: 125

Why does windows form freeze when it loads in C#?

I am new to C# and I am using windows forms. I am building an application and I am facing a strange serious problem when Form loads.

I have 2 forms:

Form1 has a button_Edit and DataGridView

Form2 has a DataGridView1 and DataGridView2

As it is shown in the code and the screen shot, in Form1 when I select a row in DataGridView then click on Button_Edit the Order number and DateTime values in DataGridView on Form1 are passed to Form2 and then Form2 opens up. Now in Form2 Load event there is a SQL query which takes Order number and DateTime to bring the relevant order details and then fill DataGridView1 and DataGridView2 in Form2.

screenShot

This code works fine and as I wanted and DataGridView1 & DataGridView2 in Form2 are filled with the right details and it works fine when Form2 loads. However, sometimes Form2 freezes after filling both DataGridView1 & DataGridView2 when Form2 loads and I can not do anything until I kill the Application using Task manager. This issue happens sometimes and it is unpredictable. I really don’t know what is wrong.

I had a look here , here and here but non of those questions related to my issue. Note that I already use try catch and it does not throw anything because the form freezes. This freeze behavior occurs in the release mode I mean after I build EXE file then I install it in a PC and here the issue happens.

Has anyone got any idea why this bad unpredictable behavior occurs? Is there anything that I should change in my code? I will be very happy to listen to any new ideas/solutions no matter how small it is, it will be very beneficial. Thank you

Upvotes: 2

Views: 1486

Answers (3)

Michael
Michael

Reputation: 11

as said the code snippet you gave us isn't well written. You should use more Try/Catch Statements to prevent crashs and freezes on your programm. This will help you to find bugs even while running the Programm in release. Furthermore you have many options to solve your problem.

1: Try to start Form2 in a second thread, then only your form2 will freeze until your sql finishes

2: As mentioned before try to use asyncronous calls to avoid freeze time of handling the sql

3: There is no direct need of a SQL Database/Connection. You could also use a Collection and create objects defined of your Products (like cola) and them bind them through the database. (If you are interested in i could show you a example)

The best way for you would be:

Add some Try/Catch Statements and get familiar with it, get familiar with Systems.Thread and try to start your form2 in a new thread if this doenst work go to step 2 and add asynchronus calls

In the end i would like to tell you, that namen your Forms "Form1" and "Form2" isn't nice could maybe you like to change it.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

I suggest using Async versions of Open (connecting to RDBMS) and ExecuteReader (query executing):

con.Open()        -> await con.OpenAsync()
q.ExecuteReader() -> await q.ExecuteReaderAsync()

this easy substitution makes UI responsible (it doesn't freeze) while connecting to RDBMS and executing the query.

// do not forget to mark Form2_Load method as async 
private async void Form2_Load(object sender, EventArgs e) {
  // Do not share the connection - create it and dispose for each call 
  using (SqlConnection con = new SqlConnection(...)) {
    await con.OpenAsync();

    string sql = 
      @"SELECT * 
          FROM Customer_Order_Details 
         WHERE Order_Number = @OrderNumber 
           AND Date_Time = @DateTime";

    // do not share command/query as well 
    using (SqlCommand q = new SqlCommand(sql, con)) {
      q.Parameters.Add("@OrderNumber", SqlDbType.Int).Value = Order_Number;
      q.Parameters.Add("@DateTime", SqlDbType.DateTime).Value = Date_Time;

      dataGridView1.Rows.Clear();
      dataGridView2.Rows[0].Cells[1].Value = 0;

      Sum = 0;

      // We actually don't want any sql adapter: 
      // all we have to do is to fetach data from cursor and append it to grids
      using (var reader = await q.ExecuteReaderAsync()) {
        while (reader.Read()) {
          dataGridView1.Rows.Add(reader[2], reader[3], reader[4]);
          Sum += Convert.ToDouble(reader[4]);  
        }
      } 
    }  
  }
}

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 61

Do SQL work on another thread. Check out Asynchronous call.

BeginInvoke

Upvotes: 2

Related Questions