明天就要回家了,过来就是明年了,所以讲学到的东西整理一下,方便以后查阅。
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;