Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread in windows programming !!!

Status
Not open for further replies.

HadiRezaee

Technical User
Mar 9, 2001
165
IR
Hi all,
I want to know what is Thread in windows programming, because i see many function about AfxGetThread and ...
 
Roughly speaking,each EXE can start many processes(ussually will start only one.).
Each process will have many threads, which can be put to perform different tasks at the same time(with or without interface signs).
There are two types of threads:
-worker thread (usully for backgrouund jobs, no message queue, hjust a main function of the thread)
-user interface threads(with a message queue for the windows it displays)

Example of worker thread:(MFC EXE project, call it MyThread)
1.Add members to your CMyThreadApp class:
CEvent eventStart1;
CEvent eventStart2;
also put #include <afxmt.h> in your stdafx.h file

2.Put this in the InitInstance of your CMyThreadApp:

CWinThread* pFirstThread=AfxBeginThread((AFX_THREADPROC)FirstThreadProc,(LPVOID)m_pMainWnd->m_hWnd,0,0,NULL);
eventStart1.SetEvent(); //to signal the thread to start
eventStart2.SetEvent(); //to signal the thread to start

3. At the begining of the .cpp file for CMyThreadApp, put this line AFTER the includes:

UINT FirstThreadProc(LPVOID pParam);

4.At he end of the same .cpp file put this:

UINT FirstThreadProc(LPVOID pParam)
{//this thread start after event1 and event2 are signaled
CMyThreadApp* pApp=(CMyThreadApp*)AfxGetApp();

HANDLE hEvents[2];
hEvents[0]=pApp->eventStart1;
hEvents[1]=pApp->eventStart2;

AfxMessageBox(&quot;First thread waits&quot;);
WaitForMultipleObjects(2,hEvents,TRUE,INFINITE);

AfxMessageBox(&quot;First thread runs&quot;);
//here you should have the body of the thread, some calculations etc.
//signal that the first thread is finished
//here you can signal to another thread
return 0;
}

Also you should read from MSDN:
-Syncronization functions
-Process and Threads overview
-Process and Threads functions

Hope this will help,s-) Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top