我对 c#、线程和表单还是很陌生。我正在编写一个小型数据采集程序。它有两个线程:主 ui 线程和一个传感器轮询/记录/图表线程。当用户单击“开始记录”按钮时,它会不断轮询传感器(通过虚拟 COM 端口),将响应写入文件,使用一些基本轮询统计信息(每秒轮询次数)更新主窗体。如果用户单击了“监视器”按钮,它会打开一个图表表单,并且轮询线程调用一个将传感器值添加到图表的方法。
我有一个运行良好的程序版本,但我发现如果我打开了多个图表(以便我可以实时查看多个传感器),图表更新会变得零星或停止,只有焦点窗口会顺利更新。(通信端口只有 56kbaud,所以它不像轮询被数据淹没。)
所以我有了制作图表线程的“聪明”想法,认为这将提供多个 UI 循环,并会在多个图表表单上产生漂亮的平滑图表。以下是简化代码;例如,在这里,图表线程由轮询线程启动,而不是在用户单击“监控”按钮时启动。它可以编译,但是当它运行时,在调用 update_chart 方法时出现交叉引用错误。
似乎我对有关线程和控制所有权的几件事有根本的误解。图表是在“图表”线程中制作的,但是当“轮询”线程调用 update_chart 方法时,代码显示 update_chart 方法正在由“main_ui”线程运行。我愿意接受任何可以让我顺利进行图表和统计更新的建议/建议。谢谢。
namespace WindowsFormsApplication1
{
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 = new Chart_Form();
public Main_Form()
{
Thread.CurrentThread.Name = "main_ui";
update_stats_delegate = new UpdateUIStatsDelegate(update_stats);
polling_thread.DoWork += polling_thread_DoWork;
charting_thread.DoWork += charting_thread_start;
}
private void start_logging_Click(object sender, EventArgs e)
{
start_polling_thread();
start_charting_thread();
}
private void start_polling_thread()
{
polling_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();
log_to_file(sensor_values);
// BeginInvoke(chart_form.update_chart_delegate, new object[] { sensor_values });
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 void update_stats(string stat)
{
string tn = Thread.CurrentThread.Name;
// this says "main_ui", but i don't get a cross-reference error
pollings_per_second.Text = stat;
}
private void start_charting_thread()
{
charting_thread.RunWorkerAsync();
}
private void charting_thread_start(object sender, DoWorkEventArgs e)
{
Thread.CurrentThread.Name = "charting";
Chart_Form 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()
{
string tn = Thread.CurrentThread.Name;
update_chart_delegate = new UpdateChartDelegate(update_chart);
this.Text = "a realtime plot of sensor values";
}
private void update_chart(string sensor_values)
{
string tn = Thread.CurrentThread.Name;
// this says "main_ui" and i get a cross reference error; set below.
int x = extract_x_value(sensor_values);
int y = extract_y_value(sensor_values);
chart1.Series[X_AXIS].Points.AddY(x); // <<--- i get a cross-reference runtime error here...
chart1.Series[Y_AXIS].Points.AddY(y);
}
}
}