MFC threading question

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.

Thanks in advance.
Last edited on
Your CDraw2::drawMonitor method must be declared as static or use a normal function (not a member of a class).
_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);

and in the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void drawMonitor(void *arg)
{
    CDraw2 cdDrawInst = (CDraw2 *) arg;
    TCHAR buf[128];
    _stprintf(buf, _T("Draw Monitor started\n"));
    OutputDebugString(buf);
    while (true)
    {
        if (g_VangleReady)
        {
            cdDrawInst->DrawLines();
            g_VangleReady = false;
        }
        else 
            Sleep(g_DrawMonitorDelay);
    }
    _stprintf(buf, _T("Draw Monitor stopped\n"));
    OutputDebugString(buf);
}


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.

Look at http://www.codeproject.com/Articles/3539/Threads-with-MFC and http://msdn.microsoft.com/en-us/library/975t8ks0(v=vs.71).aspx
You should try to do your GUI updates from the main thread. You're asking for trouble if you have multiple threads updating the screen.
Topic archived. No new replies allowed.