how to pass argument to afxbeginthread

2.3k views Asked by At

I am running a modeless dialog box using afxbeginthread. I have created a new class derived from Cwinthread and Overridden the initinstance as below.

BOOL GuiThread::InitInstance(void)
{
    CWinThread::InitInstance();
    dialog *dial = new dialog;
    this->m_pMainWnd = dial;
    dial->Create(dialog::IDD);
    dial->ShowWindow(SW_SHOWNORMAL);
    return TRUE;
}

In my main thread, I am creating a user interface thread as follows.

GuiThread *gui = (GuiThread*)AfxBeginThread(RUNTIME_CLASS(GuiThread),THREAD_PRIORITY_NORMAL,0,NULL,NULL);
WaitForSingleObject(gui->m_hThread,INFINITE);

My question is: How I can pass an argument to the GUI thread? I want to pass a string: How can I do that?

1

There are 1 answers

2
ScottMcP-MVP On

You can use the CREATE_SUSPENDED flag to create the thread object without starting the thread, then set member variables of the thread object and start the thread:

GuiThread *gui = (GuiThread*)AfxBeginThread(RUNTIME_CLASS(GuiThread),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
gui->m_sometext = sometext;
gui->ResumeThread();