mfc创建线程的三种方法

mfc创建线程的三种方法,第1张

MFC中有两类线程,分别称之为工作者线程和用户界面线程。二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环。

工作者线程没有消息机制,通常用来执行后台计算和维护任务,如冗长的计算过程,打印机的后台打印等。用户界面线程一般用于处理独立于其他线程执行之外的用户输入,响应用户及系统所产生的事件和消息等。但对于Win32的API编程而言,这两种线程是没有区别的,它们都只需线程的启动地址即可启动线程来执行任务

1、WIN的API函数CreateThread

HANDLE CreateThread(

LPSECURITY_ATTRIBUTESlpThreadAttributes, // SD

DWORDdwStackSize, // initial stack size

LPTHREAD_START_ROUTINElpStartAddress, // thread function

LPVOIDlpParameter, // thread argument

DWORDdwCreationFlags, // creation option

LPDWORDlpThreadId // thread identifier

);

//lpThreadAttributes:指向SECURITY_ATTRIBUTES型态的结构的指针。在Windows 98中忽略该参数。在Windows NT中,它被设为NULL,表示使用缺省值。

dwStackSize,线程堆栈大小,一般=0,在任何情况下,Windows根据需要动态延长堆栈的大小。

lpStartAddress,指向线程函数的指针,形式:@函数名,函数名称没有限制,但是必须以下列形式声明:

DWORD WINAPI ThreadProc (LPVOID lpParam) ,格式不正确将无法调用成功。

lpParameter:向线程函数传递的参数,是一个指向结构的指针,不需传递参数时,为NULL。

dwCreationFlags :线程标志,可取值如下

(1)CREATE_SUSPENDED-----创建一个挂起的线程,

(2)0---------------------------------表示创建后立即激活。

lpThreadId:保存新线程的id。

2、MFC的全局函数AfxBeginThread

CWinThread AfxBeginThread( AFX_THREADPROCpfnThreadProc, LPVOIDpParam, intnPriority= THREAD_PRIORITY_NORMAL, UINTnStackSize= 0, DWORDdwCreateFlags= 0, LPSECURITY_ATTRIBUTESlpSecurityAttrs= NULL );

//用于创建工作者线程 其中pfnThreadProc指向线程函数 pParam为传递给线程函数的参数

CWinThread AfxBeginThread( CRuntimeClasspThreadClass, intnPriority= THREAD_PRIORITY_NORMAL, UINTnStackSize= 0, DWORDdwCreateFlags= 0, LPSECURITY_ATTRIBUTESlpSecurityAttrs= NULL );

//用于创建用户界面线程 其中pThreadClass为CWinThread派生对象的RUNTIME_CLASS

3、MFC的CWinThread类的CreateThreat成员函数

BOOL CreateThread( DWORD dwCreateFlags = 0, UINT nStackSize = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );

//CWinThread类支持工作者线程和用户界面线程 可以将一个CWinThread派生类的CRUNTIMECLASS的指针作为参数传递给AfxBeginThread函数以创建一个用户界面线程 CWinThread类的CreateThread成员函数创建一个调用进程的地址空间中执行的线程

VC 60 创建线程的三种方法

CreateThread/ExitThread

_beginthreadex/_endthreadex

AfxBeginThread /AfxEndThread

对以上三种方式的选择:

1在使用了MFC的程序中使用AfxBeginThread函数或者CWinThread::CreateThread函数创建线程。

2在非MFC工程中,如果要创建多线程,建议使用_beginthreadex

3避免使用CreateThread函数。不使用_beginthread

4线程内部退出函数使用与创建函数配套的函数。

/这是我写的最简单的多线程程序,看懂不?/

#include <windowsh>

#include <stdioh>

//#include <strsafeh>

DWORD WINAPI ThreadProc1( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 1 \n");

//延时

for(i=0;i<200000000;i++)

{

;

}

}

}

DWORD WINAPI ThreadProc2( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 2 \n");

//延时

for(i=0;i<200000000;i++)

{

;

}

}

}

void main()

{

int i=0;

//创建线程1

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc1, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//创建线程2

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc2, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”

while(1)

{

printf("hello,this thread 0 \n");

//延时

for(i=0;i<200000000;i++)

{;}

}

}

//放在需要创建线程的地方,如主程序

//保存线程的ID。

DWORD dwThreadID = 0;

HANDLE hThread=CreateThread(NULL,0,DownloadThread,0,NULL,&dwThreadID);//创建下载线程

//全局函数

static DWORD WINAPI DownloadThread(void pArg)

{

//这里写上创建线程做什么的函数

return 0;

}

////////////等待线程结束

//等待线程结束。

DWORD dRet;

MSG msg;

while (1)

{

dRet=::MsgWaitForMultipleObjects(1,&hThread,FALSE,INFINITE,QS_ALLINPUT);

if (dRet == WAIT_OBJECT_0+1)

{

while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

else

{

break;

}

}

//删除的线程资源。

CloseHandle(hThread);

publicclassDoubleThread{\x0d\publicstaticvoidmain(String[]args){\x0d\Threadt1=newThread(){\x0d\@Override\x0d\publicvoidrun(){\x0d\for(chari='a';i回答于 2022-12-14

/这是我写的最简单的多线程程序,看懂不?/

#include <windowsh>

#include <stdioh>

//#include <strsafeh>

DWORD WINAPI ThreadProc1( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 1 \n");

//延时

for(i=0;i<200000000;i++)

{

;

}

}

}

DWORD WINAPI ThreadProc2( LPVOID lpParam )

{

int i=0,j=0;

while(1)

{

printf("hello,this thread 2 \n");

//延时

for(i=0;i<200000000;i++)

{

;

}

}

}

void main()

{

int i=0;

//创建线程1

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc1, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//创建线程2

CreateThread(

NULL, // default security attributes

0, // use default stack size

ThreadProc2, // thread function

NULL, // argument to thread function

0, // use default creation flags

NULL); // returns the thread identifier

//让主线程进入循环,主线程若退出,子线程1,2会被系统“杀死”

while(1)

{

printf("hello,this thread 0 \n");

//延时

for(i=0;i<200000000;i++)

{;}

}

}

以上就是关于mfc创建线程的三种方法全部的内容,包括:mfc创建线程的三种方法、c语言中怎样创建多线程。最好有一个例子,谢谢!!、MFC中如何创建一个线程等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9663533.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存