i know this is a repost of a previous question i asked...
c# wanting multiple ui threads but getting cross-reference errors instead
...but my followup question wasn't answered so i'm posting again for help on the new problem. i'll repeat the intro here. thanks for you indulgence.
i'm still very new at c#, threads and forms. i've written a small data acquistion program. it has two threads: a sensor polling/logging thread and a main UI thread which can chart the sensor data. when the user clicks the "start-logging" button, it continuously polls the sensors (over a virtual COM port), writes the response to a file, updates the main form with some basic polling stats (how many pollings per second). if the user has clicked a "monitor" button, it opens a charting form and the polling thread begininvokes a method that adds the sensors values to the chart. the program works well but i found that if i have multiple charts open (so that i can view multiple sensors in realtime), the chart updates become sporadic or stop and only the window with the focus updates smoothly. (the comm port is only 56kbaud so it's not like the polling is being swamped with data.)
so i got the "bright" idea to make charting threads, thinking this would provide multiple UI loops (so i could interact with each chart) and would produce nice smooth charting on multiple chart forms. below is simplified code; e.g. here, the charting thread is started with the polling thread instead of when the user clicks the "monitor" button.
the problem is that the delegate is never performed. the stats on the main form is being updated nicely, the charting form is displayed, but is unresponsive and i get the "wait" cursor when i mouse it. advice greatly appreciated. thanks.
namespace WindowsFormsApplication2
{
public partial class Main_Form : Form
{
delegate void UpdateUIStatsDelegate(string update);
UpdateUIStatsDelegate update_stats_delegate;
static BackgroundWorker polling_thread = new BackgroundWorker();
static BackgroundWorker charting_thread = new BackgroundWorker();
public static Chart_Form chart_form;
public Main_Form()
{
Thread.CurrentThread.Name = "main";
update_stats_delegate = new UpdateUIStatsDelegate(update_stats);
polling_thread.DoWork += polling_thread_DoWork;
charting_thread.DoWork += charting_thread_start;
}
private void start_polling_Click(object sender, EventArgs e)
{
// start polling thread
polling_thread.RunWorkerAsync();
// start charting plotting thread
charting_thread.RunWorkerAsync();
}
private void polling_thread_DoWork(object sender, DoWorkEventArgs e)
{
string sensor_values;
Thread.CurrentThread.Name = "polling";
while (true)
{
sensor_values = poll_the_sensors_and_collect_the_responses();
chart_form.BeginInvoke(chart_form.update_chart_delegate, new object[] { sensor_values });
pps = compute_polling_performance();
BeginInvoke(update_stats_delegate, new object[] { pps.ToString("00") });
}
}
private string poll_the_sensors_and_collect_the_responses()
{
send_command_to_sensor(sensor_id, command_to_return_current_readings);
return read_sensor_response(sensor_id);
}
private void update_stats(string stat)
{
pollings_per_second.Text = stat;
}
private void charting_thread_start(object sender, DoWorkEventArgs e)
{
Thread.CurrentThread.Name = "charting";
chart_form = new Chart_Form();
chart_form.Show();
while (charting_is_active) { }
}
}
public partial class Chart_Form : Form
{
public delegate void UpdateChartDelegate(string sensor_values);
public UpdateChartDelegate update_chart_delegate;
public Chart_Form()
{
update_chart_delegate = new UpdateChartDelegate(update_chart);
this.Text = "a realtime plot of sensor values";
}
private void update_chart(string sensor_values)
{
int x = extract_x_value(sensor_values);
int y = extract_y_value(sensor_values);
chart1.Series[X_AXIS].Points.AddY(x);
chart1.Series[Y_AXIS].Points.AddY(y);
}
}
}