在Visual Studio C ++中使用Windows窗体进行线程绘图
||
我正在使用Visual Studio C ++ 2010编写应用程序以执行数据采集并实时绘制此信息。我正在使用Windows窗体创建GUI。我同时从串行端口和DAQ卡(我有并已使用过库)中获取数据,并希望实时绘制它们。我以前在Python中完成过此操作,但是我必须使用在C ++中完成的另一个库,因此这次我不能使用Python。
我的想法是让串行端口和daq卡在单独的线程中获取数据,然后将更新的信息发送到主程序,以使用新数据更新绘图。我终于使线程正常工作,但是我似乎无法弄清楚的是如何从线程内部更新绘图,因为我所导致的崩溃。
这是我到目前为止的内容:
#pragma once
#include <math.h>
namespace PlotUpdate {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
th1 = gcnew Thread(gcnew ThreadStart(this, &Form1::th1Method));
}
delegate void UpdatePlot();
UpdatePlot^ myDelegate;
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: System::Windows::Forms::DataVisualization::Charting::Chart^ chart1;
Thread ^th1;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::Windows::Forms::DataVisualization::Charting::ChartArea^ chartArea1 = (gcnew System::Windows::Forms::DataVisualization::Charting::ChartArea());
System::Windows::Forms::DataVisualization::Charting::Series^ series1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Series());
this->button1 = (gcnew System::Windows::Forms::Button());
this->chart1 = (gcnew System::Windows::Forms::DataVisualization::Charting::Chart());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->chart1))->BeginInit();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(291, 369);
this->button1->Name = L\"button1\";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L\"button1\";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// chart1
//
chartArea1->Name = L\"ChartArea1\";
this->chart1->ChartAreas->Add(chartArea1);
this->chart1->Location = System::Drawing::Point(32, 30);
this->chart1->Name = L\"chart1\";
series1->ChartArea = L\"ChartArea1\";
series1->ChartType = System::Windows::Forms::DataVisualization::Charting::SeriesChartType::Line;
series1->Name = L\"Series1\";
series1->XValueMember = L\"xvals\";
series1->YValueMembers = L\"yvals\";
this->chart1->Series->Add(series1);
this->chart1->Size = System::Drawing::Size(669, 314);
this->chart1->TabIndex = 1;
this->chart1->Text = L\"chart1\";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(778, 415);
this->Controls->Add(this->chart1);
this->Controls->Add(this->button1);
this->Name = L\"Form1\";
this->Text = L\"Form1\";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->chart1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
static double time = 0.0;
private: System::Void th1Method()
{
for(int i=0;i<500;i++)
{
this->chart1->Series[\"Series1\"]->Points->AddXY(time, sin(time));
time += 0.1;
Thread::Sleep(1);
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
th1->Start();
}
};
}
代码编译并运行,直到启动线程,然后崩溃。我知道,我无法从其他进程更新GUI,所以我真的不知道该怎么做。我已经尝试过(并且由于没有示例代码而致歉)创建一个DataPoint对象的临时集合,然后使用TimerEvent更新该图,但是我遇到了无法在静态方法的内部使用this->表示法的问题班级。
对于这种情况有任何提示/技巧/建议吗?
没有找到相关结果
已邀请:
1 个回复
辽躺
请注意BeginInvoke调用,该调用在此处带有一个lambda函数(类型为Action,表示没有参数,也没有返回值)。此lambda函数将在UI线程中排队,然后在UI线程中执行。 有关BeginInvoke的更多信息,请参见:http://msdn.microsoft.com/zh-cn/library/0b1bf3y3.aspx