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

How to create a thread inside a class using System::Threading Visual C++

我需要在我的 class LandingPage 中创建一个线程,以便 运行 我可以 pause/play 使用按钮事件的并行任务。该平台是 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使用单独的class来定义线程中要使用的函数。我不能那样做,因为我需要 LandingPageprivate 变量。我应该如何编辑我的代码才能作为 in-class 线程工作?

更改此行:

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));

对我有用。