vc多线程-------自己的感悟,多多交流。

         我们在网上搜索vc多线程的创建,一般就如前几次转发的文章一样。

         但是,在mfc中,这样子感觉似乎有点麻烦。其实我们可以利用线程派生类的方式创建线程。利用CWinThread 进行用户界面线程的创建。

         首先,我们在mfc类中添加类thread,基类是CWinThread。这样会自动创建出thread.h,thread.cpp两个文件。内容如下:

 ///////////////////////////      thread.h           /////////////////////////////////////  

#pragma once

 

// thread

class thread : public CWinThread
{
 DECLARE_DYNCREATE(thread)

public:               //注意,电脑自动生成的是protected
 thread();           // 动态创建所使用的受保护的构造函数
 virtual ~thread();

public:
 virtual BOOL InitInstance();
 virtual int ExitInstance();
 protected:
 DECLARE_MESSAGE_MAP()
};

 ///////////////////////////      thread.cpp        /////////////////////////////////////  

// thread.cpp : 实现文件
//

#include "stdafx.h"
#include "multithread copy.h"
#include "thread.h"


// thread 
IMPLEMENT_DYNCREATE(thread, CWinThread)

thread::thread()
{
}

thread::~thread()
{
}

BOOL thread::InitInstance()
{
 // TODO: 在此执行任意逐线程初始化
 return TRUE;
}

int thread::ExitInstance()
{
 // TODO: 在此执行任意逐线程清理
 return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(thread, CWinThread)
END_MESSAGE_MAP()


// thread 消息处理程序

}

然后,在要调用的地方 初始化线程

thread* thread1;
thread1=new thread();//申请线程空间
 thread1->CreateThread();//创建线程并启用

 

        最后,根据自己的需要,在thread.h中添加需要的消息函数,并在thread.cpp中实现。在主进程中调用即可。

        不知道自己总结的是否正确,往大家指正。。。 谢谢。。。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值