Hi,
I am using C++ MFC and I wouldn't exactly call myself an expert :-). What I have is a thread drawMonitor which monitors when to draw and I would like it to call DrawLines() (which is a member function in the class) to do the drawing.
void drawMonitor(void *); //this is in the code file
void drawMonitor(void *arg)
{
TCHAR buf[128];
_stprintf(buf, _T("Draw Monitor started\n"));
OutputDebugString(buf);
while (true)
{
if (g_VangleReady)
{
//DrawLines();
g_VangleReady = false;
}
else
Sleep(g_DrawMonitorDelay);
}
_stprintf(buf, _T("Draw Monitor stopped\n"));
OutputDebugString(buf);
}
void CDraw2::DrawLines()
{
//Code here
}
I am starting my thread using _beginthread(drawMonitor, 0, (void*)12);
This builds and executes ok, but the DrawLines is commented out. When I remove the comments I get the error message: C3861: 'DrawLines': identifier not found. When I attempt to add drawMonitor to the class:
1 2
void CDraw2::drawMonitor(void *); //this is in the header file, all else is in the code file
void CDraw2::drawMonitor(void *arg) { /* same code as above but DrawLines is not commented out */ } //in code file
I am now starting my thread using _beginthread(CDraw2::drawMonitor, 0, (void*)12);
I now get an error message C3867: 'CDraw2::drawMonitor': function call missing argument list; use '&CDraw2::drawMonitor' to create a pointer to member
I am not much a C++ progreammer, but we are trying to throw a demonstrator together which requires the use of C++ MFC. Any help with what I am doing wrong would be much appreciated as I've spent many hours trying to puzzle through this.
_beginthread(drawMonitor, 0, (void*)12);
is good except you need to pass instance/object of the class CDraw2 as argument (why is the "12" even passed? its not used anyway)
like _beginthread(drawMonitor, 0, (void*)cdDrawObject);
To make it work like _beginthread(CDraw2::drawMonitor, 0, (void*)12);
You need to make CDraw2::drawMonitor a static function
Now this should make it work, but if you are using MFC anyway why don't you use CWinThread/AfxBeginThread instead of _beginthread? Why mix plain windows API and MFC?
Also you need to look at synchronising threads, you are accessing global variables from the thread.