在Visual Studio c++中使用Windows窗体进行线程绘图
Threaded plotting in Visual Studio C++ with Windows Forms
我正在使用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,所以我真的不知道我应该怎么做。我已经尝试过(我为没有示例代码而道歉)创建数据点对象的临时集合,然后使用TimerEvent更新情节,但我遇到了无法在类的静态方法中使用此->符号的问题。
对于这种情况有什么提示/技巧/建议吗?除非我错了,否则你试图从一个不是UI线程的线程内修改UI,这是一个错误。
如果是这种情况,你应该使用你的Form的BeginInvoke方法从UI线程中执行代码。
我不熟悉c++/CLI + WinForms代码,所以我无法为您提供代码更正,但在c#中,它应该是这样的:
private void th1Method()
{
for(int i=0;i<500;i++)
{
this.BeginInvoke
((Action)(
() =>
{
this.chart1.Series["Series1"].Points.AddXY(time, sin(time));
time += 0.1;
}
)) ;
Thread.Sleep(1);
}
}
注意BeginInvoke调用,它在这里接受一个lambda函数(类型为Action,意味着没有参数,也没有返回值)。这个lambda函数将在UI线程中排队,然后在UI线程中执行。
有关BeginInvoke的更多信息,请参见:http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- std::线程在 Windows 窗体中不可用
- c++多线程窗口GUI(访问窗体)
- 单击主窗体按钮时线程执行"stops"
- 正在尝试从窗体运行线程
- 将不同的线程方法组合到Windows窗体应用程序中
- 在Visual Studio c++中使用Windows窗体进行线程绘图
- windows窗体中多线程和后台工作线程的几个问题