Things You'll Need:
- Microsoft Visual Studio IDE
- Book on MFC, such as "Programming Windows with MFC" by Jeff Prosise
-
Step 1
Start Microsoft Visual Studio. Create a dialog-based MFC project by clicking "File" from the upper menu and then "New." After clicking the "Projects" tab, select "MFC AppWizard(exe)" and type "WThread" in the "Project name:" edit box. Click "OK." In the next wizard screen, select "Dialog based," then click "Finish" and "OK." The wizard generates a default dialog box and its associated source files.
-
Step 2
Make a function that will act as the Worker thread for this MFC tutorial. Open the WorkerThreadDlg.cpp file by expanding the "Source Files" folder in the left pane and double-clicking the file. Copy/paste the following source code at the end of the file:
void ThreadProcess(LPVOID param) {
CFile f;
f.Open("proof.txt", CFile::modeCreate | CFile::modeWrite);
CString str;
for(int i=0; i<50; i++) {
str.format("Num: %d", i);
f.Write(str, str.GetLength());
}
f.Close();
} -
Step 3
Activate the Worker thread (the function in Step 2) in an event. For an event, use the click of the default dialog's "OK" button. Get back into design mode by pressing Ctrl+F4. Select the "OK" button and double-click it. Press "OK" when the "Add Member Function" box appears. Copy and paste the following code inside the braces of "CWorkerThreadDlg::OnOK()."
AfxBeginThread(ThreadProcess, NULL, THREAD_PRIORITY_TIME_CRITICAL
, 0, 0, NULL);
MessageBox("Worker Thread Activated", NULL, NULL); -
Step 4
Compile and run the application. When you click "OK," a message box will appear stating that the background Worker thread has executed. Go to the project directory and see the written text in "proof.txt."






