The code below is the complete program for a simple application that I want to periodically update a DataGridView every few seconds. I created a thread in my form load event handler, and moved this original Fill() call...
this.alarmsTableAdapter.Fill(this.pattyDataSet.Alarms);
... from the event handler into the thread code, storing the object reference into myForm for the thread's use.
There are two problems with this program:
(1) The DataGridView never fills with data until I press my stop button (which invokes the toolStripButton1_Click handler below). The MessageBox.Show is what seems to force a refresh of the DataGridView.
(2) When my grid fills with data by pressing the stop button, only the very first cell of the grid gets a value. Yet when I do a preview data on the TableAdapter or the BindingSource I get multiple rows and columns filled in.
Can someone fix these? I am a C# newbie so I am betting that I am doing something wrong that should be obvious to the right pair of eyes...
public partial class Form1 : Form
{
private static bool running = true;
private static Form1 myForm;
private Thread t = new Thread(new ThreadStart(ThreadProc));
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
myForm = this;
t.Start();
Thread.Sleep(0);
}
public static void ThreadProc()
{
while (running)
{
myForm.alarmsTableAdapter.Fill(myForm.myDataSet.Alarms);
Thread.Sleep(2000); // pause
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
running = false;
t.Abort();
t.Join(); // not sure if this is needed...
MessageBox.Show("stopped");
}
}