如何使用System :: Threading Visual C ++在类中创建线程

时间:2018-03-23 05:48:36

标签: multithreading winforms c++-cli msdn

我需要在类LandingPage中创建一个线程,以便运行一个可以使用按钮事件暂停/播放的并行任务。该平台是Windows,我正在使用WinForms。

我的代码:

public ref class LandingPage : public System::Windows::Forms::Form {
    public:
        LandingPage(void)
        {
            InitializeComponent();
            this->thr = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(LandingPage, &LandingPage::CaptureImagesToLoad));
        }
    private: static System::Threading::Thread^ thr;
    private: System::Void CaptureImagesToLoad() {
        while (true) {
            Fove::SFVR_BitmapImage image = this->dataCapture->GetEyeImageData();
            this->dataCapture(&image, "C:\\Users\\Kapil\\Documents\\Dataset\\buffer.bmp");
            this->eyePictureBox->ImageLocation(bufferingLocation);
        }
    }
};

我得到的错误是:

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   'initializing': cannot convert from 'FOVEImageCapture::LandingPage' to 'FOVEImageCapture::LandingPage ^'    FOVEImageCapture    c:\users\kapil\desktop\project1\project1\myform.h   43  
Error   C3754   delegate constructor: member function 'FOVEImageCapture::LandingPage::CaptureImagesToLoad' cannot be called on an instance of type 'FOVEImageCapture::LandingPage'  FOVEImageCapture    c:\users\kapil\desktop\project1\project1\myform.h   43  
Error   C2512   'System::Threading::Thread::Thread': no appropriate default constructor available   FOVEImageCapture    c:\users\kapil\desktop\project1\project1\myform.h   43  

我发现online的示例使用单独的类来定义要在线程中使用的函数。我不能这样做,因为我需要LandingPage private个变量。我应该如何编辑我的代码以作为类内线程?

1 个答案:

答案 0 :(得分:0)

更改此行:

this->thr = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(LandingPage, &LandingPage::CaptureImagesToLoad));

this->thr = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &LandingPage::CaptureImagesToLoad));

为我工作。