Click here to Skip to main content
15,906,455 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Simplest way to start a MFC program from a MFC program Pin
Member 28839418-Nov-11 23:01
Member 28839418-Nov-11 23:01 
GeneralRe: Simplest way to start a MFC program from a MFC program Pin
Richard MacCutchan18-Nov-11 23:06
mveRichard MacCutchan18-Nov-11 23:06 
GeneralRe: Simplest way to start a MFC program from a MFC program Pin
Member 28839418-Nov-11 23:18
Member 28839418-Nov-11 23:18 
QuestionHow to inhibit parent window from blocking when using CSocket/CTimeoutSocket Pin
Member 28839418-Nov-11 20:53
Member 28839418-Nov-11 20:53 
AnswerRe: How to inhibit parent window from blocking when using CSocket/CTimeoutSocket Pin
Code-o-mat20-Nov-11 2:23
Code-o-mat20-Nov-11 2:23 
QuestionReverse Triangle Star Pin
AREYASB18-Nov-11 17:21
AREYASB18-Nov-11 17:21 
AnswerRe: Reverse Triangle Star Pin
Rajesh R Subramanian18-Nov-11 17:51
professionalRajesh R Subramanian18-Nov-11 17:51 
QuestionRe: Reverse Triangle Star Pin
David Crow19-Nov-11 15:46
David Crow19-Nov-11 15:46 
AnswerRe: Reverse Triangle Star Pin
AREYASB19-Nov-11 21:28
AREYASB19-Nov-11 21:28 
GeneralRe: Reverse Triangle Star Pin
enhzflep20-Nov-11 1:57
enhzflep20-Nov-11 1:57 
GeneralRe: Reverse Triangle Star Pin
AREYASB20-Nov-11 21:51
AREYASB20-Nov-11 21:51 
QuestionRe: Reverse Triangle Star Pin
David Crow20-Nov-11 17:14
David Crow20-Nov-11 17:14 
AnswerRe: Reverse Triangle Star Pin
AREYASB20-Nov-11 21:48
AREYASB20-Nov-11 21:48 
QuestionRe: Reverse Triangle Star Pin
David Crow21-Nov-11 4:13
David Crow21-Nov-11 4:13 
QuestionStar Shear Pin
AREYASB18-Nov-11 15:58
AREYASB18-Nov-11 15:58 
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 

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.