Search code examples
multithreadingwinformsc++-climsdn

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


I need to create a thread inside my class LandingPage in order to run a parallel task that I can pause/play using a button event. The platform is Windows and I am working on WinForms.

My Code:

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

Errors I am getting are :

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  

The example I found online uses a separate class to define the function to be used in the thread. I can't do that as I require LandingPage's private variables. How should I edit my code to be workable as an in-class thread?


Solution

  • Changing this line :

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

    to

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

    worked for me.