• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

C# Application Freezes if too many Results are returned to a Grid View

Hey all,

Im having some trouble with a C# application I have written. Usually I am able to find an answer on this excellent forum but unfortunately Im not quite sure how to phrase a search to help me find this answer.

The application is a C# app written on Visual Studio 2008 using .Net 2.0.

The Application:
Basically I have a Gridview embedded in a Windows Form with textbox and button control. Once the button is pressed a background worker thread begins that uses the textbox.text entry to search a SQL database and return the results as a selection of rows. The Form also has a status bar that runs to display the progress of the query. Once completed the background worker calls a delegate method to refresh the GridView and Display the results.

My Issue:
If the SQL Query returns a list of results that is larger than the display window the application freezes and becomes non responsive. Essentially if I maximise the app to full screen the grid view can display up to 25 records. If there are 24 or less results the app works without issue, Over 25 and it fails.

Strangely it will always work without issue no matter how many results are returned when run from Visual Studio (debug or release), however if I double click the exe in Windows explorer it fails. My code is below. I am stuck at this point as I dont quite know why it is working in VS but not when I run it standalone. Any help or direction would be appreciated.

//Delegate Declaraions
      public delegate void RefreshGUIDelegate1();
      public RefreshGUIDelegate1 myDelegate1;
 
//Button that shows the Progress Bar and begins the Background Thread
private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            myDelegate1 = new RefreshGUIDelegate1(DoRefreshGUIDelegate1);
            toolStripProgressBar1.Visible = true;
            Application.DoEvents();
            backgroundWorker1.RunWorkerAsync();
            timer1.Start();
        }
 
//Background Worked DOWork function that returns the results to the database
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                this.validation_EDDataSet1.Clear();
                this.spSearchEADProductFamilyNameTableAdapter.ClearBeforeFill = true;
                this.spSearchEADProductFamilyNameTableAdapter.Fill(this.validation_EDDataSet1.spSearchEADProductFamilyName, textBox1.Text.ToString());
            }
            catch (Exception msg5)
            {
                MessageBox.Show("Database Error: " + msg5.Message.ToString());
            }
 
        }
//Run Worker Completed that turns off the Progress Bar and calls the Delegate
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar1.Visible = false;
            timer1.Stop();
            Application.DoEvents();
            this.Invoke(myDelegate1);
        }
//Delegate that Refreshes the Grid View with Data from the Table Adapter
        public void DoRefreshGUIDelegate1()
        {
            try
            {
                
                this.spSearchEADProductFamilyNameBindingSource.ResetBindings(false);
                toolStripStatusLabel1.Text = "Number of Records Returned = " + dataGridView1.RowCount.ToString();
                Application.DoEvents();
            }
            catch (Exception msg4)
            {
                MessageBox.Show("Database Error: " + msg4.Message.ToString());
            }
 
        }
//Tick Function for the Progress Bar
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = DateTime.Now.Subtract(StartDate);
            string sTime = " Updating..." + ts.Minutes.ToString("00") +
                ":" + ts.Seconds.ToString("00") +
                ":" + ts.Milliseconds.ToString("000");
            toolStripStatusLabel1.Text = sTime;
            if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)
            {
                toolStripProgressBar1.Value = 0;
            }
            toolStripProgressBar1.PerformStep();
 
        }

Open in new window

0
jkobrien
Asked:
jkobrien
  • 3
  • 2
1 Solution
 
angus_young_acdcCommented:
A small solution would be to "page" the results.  E.g. set it so that 10 results are displayed, and if you click Next the next 10 are shown.  That should solve your latency issue.  

It does also seem like your thread is busy.  That means the UI isn't able to respond to any commands.  You could try an Application.DoEvents() to force it though.
0
 
jkobrienAuthor Commented:
Thanks - I have tried the DoEvents() call but unfortunately it didn't have any effect. I will try the paging option and see if that resolves it.
0
 
angus_young_acdcCommented:
How did it go?
0
 
jkobrienAuthor Commented:
No luck unfortunately. Same error occured with paging. I was forced to rewrite the app and remove the Progress bar. I use the Cursor Wait command to inform the user that the app is working in the background. Not ideal but it does the job needed.
0
 
jkobrienAuthor Commented:
No fix to the problem was found. Progress Bar removed and application form rewritten to bypass the issue
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now