Getting rid of this error...
-
I am doin the following...
class ThreadTest
{
public:
ThreadTest();
void end();
DWORD WINAPI Write_data(LPVOID);
DWORD ThreadID;
void run();
private:
HANDLE hThread;
bool keepRunning;
};ThreadTest::ThreadTest()
{
keepRunning = true;
}void ThreadTest::run()
{
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
}void ThreadTest::end()
{
keepRunning = false;
}DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
{
while(keepRunning)
{
printf("I'm a thread running");
Sleep(1000);
}
return 0;
}void main()
{
ThreadTest *t = new ThreadTest();
t->run();
Sleep(10000);
t->end();
}On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS
-
I am doin the following...
class ThreadTest
{
public:
ThreadTest();
void end();
DWORD WINAPI Write_data(LPVOID);
DWORD ThreadID;
void run();
private:
HANDLE hThread;
bool keepRunning;
};ThreadTest::ThreadTest()
{
keepRunning = true;
}void ThreadTest::run()
{
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
}void ThreadTest::end()
{
keepRunning = false;
}DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
{
while(keepRunning)
{
printf("I'm a thread running");
Sleep(1000);
}
return 0;
}void main()
{
ThreadTest *t = new ThreadTest();
t->run();
Sleep(10000);
t->end();
}On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS
The function you pass to the CreateThread function can't be a member function of a class (the prototype is different). So, either use a non-member function or a static function. In any case, you won't be able to access the non-static member variables. To solve this problem, pass the this pointer in the lpParameter parameter of CreateThread. It will be passed to your function and you'll be able to cast it back to your class pointer there and call a public method from there.
Cédric Moonen Software developer
Charting control [v1.4] -
I am doin the following...
class ThreadTest
{
public:
ThreadTest();
void end();
DWORD WINAPI Write_data(LPVOID);
DWORD ThreadID;
void run();
private:
HANDLE hThread;
bool keepRunning;
};ThreadTest::ThreadTest()
{
keepRunning = true;
}void ThreadTest::run()
{
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) Write_data,NULL,0,&ThreadID);
}void ThreadTest::end()
{
keepRunning = false;
}DWORD WINAPI ThreadTest::Write_data(LPVOID lpParam)
{
while(keepRunning)
{
printf("I'm a thread running");
Sleep(1000);
}
return 0;
}void main()
{
ThreadTest *t = new ThreadTest();
t->run();
Sleep(10000);
t->end();
}On compiling I get... error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Please help me out with this....THANKS
By the way, instead of using sleep, you could be much more efficient when you want to stop the thread if you use events (using CreateEvent function). When you want to stop your thread, you can signal the event. In your thread function, you can wait on the event instead of sleeping (and provide a timeout of 1000 msec) and check the return of the WaitForSingleObject to see if it is a timeout (in which case you continue your loop) or if the event has been signaled (in which case you exit your loop). This way, your thread will exit immediately and not wait until your Sleep finished.
Cédric Moonen Software developer
Charting control [v1.4]