Click here to Skip to main content
15,896,606 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Star Shear Pin
David Crow1-Dec-11 7:43
David Crow1-Dec-11 7:43 
QuestionUnderstanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx18-Nov-11 6:47
professionaljkirkerx18-Nov-11 6:47 
AnswerRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
David Crow18-Nov-11 7:05
David Crow18-Nov-11 7:05 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx18-Nov-11 7:47
professionaljkirkerx18-Nov-11 7:47 
AnswerRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Chuck O'Toole18-Nov-11 7:46
Chuck O'Toole18-Nov-11 7:46 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx18-Nov-11 8:04
professionaljkirkerx18-Nov-11 8:04 
AnswerRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Chuck O'Toole18-Nov-11 10:01
Chuck O'Toole18-Nov-11 10:01 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
enhzflep18-Nov-11 15:27
enhzflep18-Nov-11 15:27 
I understand that other's code can be more confusing than helpful at times, with this in mind I'll present this simple example in the hope it's not confusing.

I've just created the most simple dialog app I can. There's a single dialog with a test button and a quit button. Hitting the test button starts 10 threads, each with a different timeout value. Upon timeout, the thread sends a message to the main window which then sounds a message beep.

Hope it's of use..

C++
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <process.h>
#include "resource.h"
#include <stdio.h>



#define WM_THREAD_COMPLETE WM_USER+1

typedef void (*voidFuncPtr)(void*);

typedef struct
{
    int delayMs;
    int myThreadId;
    voidFuncPtr callbackFunc;
    long winThreadId;
    uintptr_t handle;
} threadHeader_t;


HINSTANCE hInst;
HWND mainDlgHwnd;
threadHeader_t threadList[10];


unsigned int __stdcall myThreadFunc(void *data)
{
    threadHeader_t *header;
    header = (threadHeader_t *)data;
    printf("In thread func %d, delay %dMs\n", header->myThreadId, header->delayMs);
    Sleep(header->delayMs);
    if (header->callbackFunc)
        header->callbackFunc(header);
    return ERROR_SUCCESS;
}

void myCompletionCallback(void *data)
{
    threadHeader_t *header;
    header = (threadHeader_t *)data;
    PostMessage(mainDlgHwnd, WM_THREAD_COMPLETE, header->myThreadId, 0);
}

void onTestBtn()
{
    int i;

    for (i=0; i<10; i++)
    {
        threadList[i].callbackFunc = myCompletionCallback;
        threadList[i].delayMs = (10-i)*1000;
        threadList[i].myThreadId = i;
        threadList[i].handle = _beginthreadex(NULL, 0, myThreadFunc, &threadList[i], 0, (unsigned *)&threadList[i].winThreadId);
    }
}


BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            mainDlgHwnd = hwndDlg;
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_THREAD_COMPLETE:
            MessageBeep(MB_OK);
            return true;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_BTN_TEST:
                    onTestBtn();
                    return TRUE;

                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Richard MacCutchan18-Nov-11 21:57
mveRichard MacCutchan18-Nov-11 21:57 
AnswerRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Erudite_Eric18-Nov-11 21:55
Erudite_Eric18-Nov-11 21:55 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx19-Nov-11 7:27
professionaljkirkerx19-Nov-11 7:27 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Erudite_Eric20-Nov-11 2:10
Erudite_Eric20-Nov-11 2:10 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx20-Nov-11 7:27
professionaljkirkerx20-Nov-11 7:27 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Erudite_Eric20-Nov-11 23:38
Erudite_Eric20-Nov-11 23:38 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
jkirkerx21-Nov-11 7:18
professionaljkirkerx21-Nov-11 7:18 
GeneralRe: Understanding wndproc, callbacks and threads, win32 c++ Pin
Erudite_Eric21-Nov-11 21:01
Erudite_Eric21-Nov-11 21:01 
QuestionHow to disable context-menu web browser control Pin
belfd18-Nov-11 4:28
belfd18-Nov-11 4:28 
QuestionCapturing screen content behind an open window Pin
ryan218917-Nov-11 10:38
ryan218917-Nov-11 10:38 
AnswerRe: Capturing screen content behind an open window Pin
Richard MacCutchan17-Nov-11 11:12
mveRichard MacCutchan17-Nov-11 11:12 
AnswerRe: Capturing screen content behind an open window Pin
Bernhard Hiller18-Nov-11 1:31
Bernhard Hiller18-Nov-11 1:31 
AnswerRe: Capturing screen content behind an open window Pin
Software_Developer18-Nov-11 5:07
Software_Developer18-Nov-11 5:07 
GeneralRe: Capturing screen content behind an open window Pin
ryan218921-Nov-11 5:04
ryan218921-Nov-11 5:04 
QuestionSelLocale in C++ Pin
CliffRat17-Nov-11 7:44
CliffRat17-Nov-11 7:44 
AnswerRe: SelLocale in C++ Pin
Richard MacCutchan17-Nov-11 8:32
mveRichard MacCutchan17-Nov-11 8:32 
GeneralRe: SelLocale in C++ Pin
CliffRat17-Nov-11 10:23
CliffRat17-Nov-11 10:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.