VC中的一些技术

明天就要回家了,过来就是明年了,所以讲学到的东西整理一下,方便以后查阅。

1、锁

afxmt.h //包含头文件
CCriticalSection m_Lock; //声明
m_Lock.Lock();//使用
2、获取实例

CWdsrvApp* pApp=(CWdsrvApp*)AfxGetApp(); //主实例
CAppThread* pAppThread; //类

3、开线程的办法

3.1 利用mfc类
为了使用的方便,这里直接把模板贴出来,下次好找一点

#pragma once
// PredictLoad.h : header file
/
// CPredictLoad thread

class CPredictLoad : public CWinThread
{
	DECLARE_DYNCREATE(CPredictLoad)
protected:
public:
	CPredictLoad();           // protected constructor used by dynamic creation
public:	
// Operations

<pre name="code" class="cpp">public: 
HANDLE m_hProcessEvent;private:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CPredictLoad)public:virtual BOOL InitInstance();virtual int ExitInstance();//}}AFX_VIRTUAL// Implementationprotected:public:virtual ~CPredictLoad();// Generated message map functions//{{AFX_MSG(CPredictLoad)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSGDECLARE_MESSAGE_MAP()};/

 


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

#include "stdafx.h"
#include "PredictLoad.h"

IMPLEMENT_DYNCREATE(CPredictLoad, CWinThread)

CPredictLoad::CPredictLoad()
{<pre name="code" class="cpp"><span style="white-space:pre">	</span>m_bAutoDelete = FALSE;
<span style="white-space:pre">	</span>m_hProcessEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}CPredictLoad::~CPredictLoad(){
CloseHandle(m_hProcessEvent);}BOOL CPredictLoad::InitInstance(){
<span style="white-space:pre">	</span>while (1) 
	{			
		CTime timeNew = CTime::GetCurrentTime();
		TIMESTAMP_STRUCT tsTime;
		tsTime.second = timeNew.GetSecond();
		DWORD dwWait = (5-(tsTime.second%5))*1000l;                     //这种取值方式需要考虑下,应该是取一个定值的       

		//Wait 5 seconds
		if (WaitForSingleObject(m_hProcessEvent,dwWait) != WAIT_TIMEOUT) //这里是留给外部信号触发,5秒的时间
			break;  //Stop Run<span style="white-space:pre">					</span>//但是在有的时候,必须当一个功能触发成功,才能开启线程的时候显得很重要
<span style="white-space:pre">										</span>//这种方式条件为:在5秒内有信号触发,则执行下面的程序,否则,停止线程。
<span style="white-space:pre">										</span>//当然,如果要无限等待信号的话,可以讲dwWait改为INFINITE
<span style="white-space:pre">										</span>//触发函数为SetEvent(m_pPreditLoad->m_hProcessEvent);
		timeT = CTime::GetCurrentTime();
		tsTime.year = timeT.GetYear();
		tsTime.month = timeT.GetMonth();
		tsTime.day = timeT.GetDay();
		tsTime.hour = timeT.GetHour();
		tsTime.minute = timeT.GetMinute();
		tsTime.second = timeT.GetSecond();
<span style="white-space:pre">	</span>}
return TRUE;}int CPredictLoad::ExitInstance(){// TODO: 在此执行任意逐线程清理return CWinThread::ExitInstance();}BEGIN_MESSAGE_MAP(CPredictLoad, CWinThread)END_MESSAGE_MAP()// CPredictLoad 消息处理程序
 




public: HANDLE m_hProcessEvent;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值