Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > thread make hang dataGridView
 

thread make hang dataGridView

i fill my datagriedview by using a thread :
while (th_findnewmess.IsAlive)
{
Thread.Sleep(10000);
string[] tmpstr = new string[]{
"a",
"b",
"c"
};
dataGridView1.Rows.Add(tmpstr);
dataGridView1.Invalidate();
dataGridView1.Update();
}
and scrolbars set on vertically bu when rows number go up my datagrid view is hang and by mouse can't scroll it
????????
Thanks Motevallizadeh
motevallizadeh  Saturday, July 25, 2009 12:57 PM
Calling user interface controls across threads is not allowed.

The following shows working code:

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(ThreadProc);
            t.Start();
        }

        private void ThreadProc()
        {
            while (true)
            {
                Thread.Sleep(1000);
                string[] tmpstr = new string[]{
                    "a",
                    "b",
                    "c"
                };

                dataGridView1.Invoke(new MethodInvoker(delegate
                {
                    // Put code to run on the UI thread here.
                    dataGridView1.Rows.Add(tmpstr);
                }));
            }
        }
Be aware that calling back to the UI thread in this manner can be a source of performance problems. But a rate of once in 10 seconds is nowhere near the rate that causes problems. Many times per second is problematic. I'm just bringing this up because it is acommon placewhere people run into problems with this technique. In such cases, it is best to consolidate updates or evenuse a timer thatperiodically pulls any new data from a buffer populated by the background thread.
  • Marked As Answer bymotevallizadeh Sunday, July 26, 2009 5:24 AM
  • Proposed As Answer byVinilV Saturday, July 25, 2009 7:22 PM
  •  
BinaryCoder  Saturday, July 25, 2009 6:36 PM
Calling user interface controls across threads is not allowed.

The following shows working code:

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(ThreadProc);
            t.Start();
        }

        private void ThreadProc()
        {
            while (true)
            {
                Thread.Sleep(1000);
                string[] tmpstr = new string[]{
                    "a",
                    "b",
                    "c"
                };

                dataGridView1.Invoke(new MethodInvoker(delegate
                {
                    // Put code to run on the UI thread here.
                    dataGridView1.Rows.Add(tmpstr);
                }));
            }
        }
Be aware that calling back to the UI thread in this manner can be a source of performance problems. But a rate of once in 10 seconds is nowhere near the rate that causes problems. Many times per second is problematic. I'm just bringing this up because it is acommon placewhere people run into problems with this technique. In such cases, it is best to consolidate updates or evenuse a timer thatperiodically pulls any new data from a buffer populated by the background thread.
  • Marked As Answer bymotevallizadeh Sunday, July 26, 2009 5:24 AM
  • Proposed As Answer byVinilV Saturday, July 25, 2009 7:22 PM
  •  
BinaryCoder  Saturday, July 25, 2009 6:36 PM

You can use google to search for other answers

Custom Search

More Threads

• Adding records usnig binding navigator on datagridview
• How to select more than one rows by pressing ctrl key and drag it to the DestinationGrid(GridView)
• Griview Problem
• Populate a listview from a datatable
• the progress bar didn't changed when the progresschanged occur of the BackgroundWorker
• Checkbox Click command
• OleDbCommand insert command ends with bool value
• Combo Box Questions
• bindinglist for binding the datagrid view in winforms
• Update of DataAdapter returns Dynamic SQL generation is not supported against multiple base tables???