在Visual Studio c++中使用Windows窗体进行线程绘图

Threaded plotting in Visual Studio C++ with Windows Forms

本文关键字:窗体 线程 绘图 Windows Studio Visual c++      更新时间:2023-10-16

我正在使用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