0
votes

I am writing a sample program with threading. I have a class called CamThread defined as follows in CamThread.h

public ref class CamThread{

public:
    CamThread();
    void takeImages();

private:
    Driver^ driver;
};

CamThread.cpp file looks like:

using namespace System::Threading;

CamThread::CamThread(){

Driver^ newdriver;
newdriver = (Driver^)gcnew ZylaDriver();
newdriver->initCamera();

Thread^ acquireThread = gcnew Thread(gcnew ParameterizedThreadStart(this,&CamThread::takeImages));
acquireThread->Start;
};

void CamThread::takeImages(){
//Additional code.}

Main function is

int main(int argc, char* argv[]) {

CamThread^ cameraThread = gcnew CamThread();

cout << endl << "Press enter to close" << endl;
cin.ignore();
return 0;
}

The problem I'm having is that in CamThread.cpp Visual Studio 2013 has an issue with &CamThread::takeImages in ParameterizedThreadStart. It is giving me "Error: Invalid delegate initializer -- function does not match the delegate type".

It seems like I'm constructing the delegate &CamThread::takeImages incorrectly, but many of the examples I've found seem to use a similar construction (e.g. https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart%28v=vs.110%29.aspx).

I've been having trouble finding other good examples.

2
As ParameterizedThreadStart name suggests, the delegate should have been able to take parameter. Which means: void takeImages(Object^); - Milan

2 Answers

3
votes

ParameterizedThreadStart class requires the delegate has a parameter. So the takeImages method should have declaration as:

void takeImages(Object^obj)

Consequently, when you want to start the thread, you need to pass a parameter:

acquireThread->Start(someParameter);

But if you don't need to pass a parameter, then just leave the method takeImages as it is, and create the thread using ThreadStart instead of ParameterizedThreadStart:

Thread^ acquireThread = gcnew Thread(gcnew ThreadStart(this, &CamThread::takeImages));

and start it with:

acquireThread->Start();
-3
votes
dt->ColumnChanging += gcnew DataColumnChangeEventHandler(this, Project1::MyForm::ppp);

then

private: void ppp(System::Object^  sender, System::Data::DataColumnChangeEventArgs^ e)
{

}

That is DataColumnChangeEventArgs matches with DataColumnChangeEventHandler.