2011-04-17 113 views
1

我正在使用Visual Studio C++ 2010编写一个应用程序来执行数据采集并实时绘制这些信息。我正在使用Windows窗体来创建GUI。我正在从串行端口和DAQ卡(我有图书馆和使用过的)中获取数据,并且想要实时绘制它们。我之前在Python中已经完成了这个工作,但是我必须使用另一个用C++完成的库,所以这次我不能使用Python。用Windows窗体在Visual Studio C++中绘制线程图

我的想法是让串口和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-> notation的问题班上。

这种情况的任何提示/提示/建议?

回答

1

除非我错了,否则您尝试从不是UI线程的线程中修改UI,这是一个错误。

如果是这种情况,您应该使用表单的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

+0

我已经看了调用,它至少会更新该地块最有可能的工作,但我不知道现在是如何访问类的方法和成员从螺纹方法内部? – Nate 2011-04-18 13:55:22

+0

@Nathaniel Hayes:'Invoke'会阻止你的工作线程,直到UI更新,这可能不是你想要的(你不希望你的工作线程被一些UI操作阻止,是吗?)。 'BeginInvoke'会让UI线程在可用时更新自己,同时释放您的工作线程。在大多数情况下(如果不是全部的话),我会使用'BeginInvoke'。 – paercebal 2011-04-18 20:40:43

+0

@Nathaniel Hayes:'th1Method'是该类的非静态私有方法,因此它可以访问'this'。上面的C#代码使用了一个lambda函数,它捕获'this'指针,并使用它来访问它的数据。所以一切都很好。我猜你应该可以在C++/CLI中执行相同的操作(至少在Visual C++ 2010中使用Lambda),如果没有,我想你可以使用简单的委托/回调,无论怎样做)。由于我从来没有真正使用C++/CLI,并且由于我无法在家中访问Windows平台,所以我无法告诉您更多。抱歉。 – paercebal 2011-04-18 20:41:13

相关问题