VC++技术内幕(windows编程篇)(16)
2)一旦调用CWnd::SetTimer启动了计时器,则WM_TIMER消息会被持续发送到指定的窗口,直至调用CWnd::KillTimer函数或计时器窗口被取消为止。
3)由于WINDOWS非实时OS,如果时间间隔少于100毫秒,计时器可能不准。 4)OS发送计时器消息的时候,如果队列里已经有了计时器消息,则不会把同样的计时器消息放进消息队列里。
说明:
1)CWnd::SetTimer
UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );
//Return The timer identifier of the new timer if the function is successful. An
application passes this value to the KillTimer member function to kill the timer. Nonzero if successful; otherwise 0.
//nIDEvent:Specifies a nonzero timer identifier. //nElapse:Specifies the time-out value, in milliseconds.
//lpfnTimer:Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the
WM_TIMER messages are placed in the application‘s message queue and handled by the CWnd object.
//CWnd::SetTimer Installs a system timer. A time-out value is specified, and every time a time-out occurs, the system posts aWM_TIMER message to the installing
application‘s message queue or passes the message to an application-defined TimerProc callback function.
2)CWnd::KillTimer
BOOL KillTimer( int nIDEvent );
//Return value:Return nozero if the event is killed.It is 0 if the KillTimer member function could not find the specified timer event.
//CWnd::KillTimer Kills the timer event identified by nIDEvent from the earlier call to SetTimer. Any pending WM_TIMER messages associated with the timer are removed from the message queue.
5,在引入多线程编程之前,WINDOWS程序员曾使用空状态处理来完成一些后台任务。(这里是OnIdle函数)
1)应用程序框架在框架消息处理循环中调用CWinApp::OnIdle(虚函数),可以重载它来处理后台任务。通常,一但OnIdle函数完成了它的工作,就要等到下次应用程序消息队列空了后才被调用。基类的OnIdle会更新工具栏按钮和状态栏指示器,并清除各种临时对象指针。
2)重载OnIdle函数可以用来更新用户界面,很有实际意义。(注:在重载OnIdle函数中一定要调用基类的OnIdle函数哦)
3)如果用户运行在一个模式对话框或正在使用菜单,则这时OnIdle函数不被调用。但我们可以在框架类中添加WM_ENTERIDLE消息控制函数来在上叙情况下使用后台处理。(注意,这里是在框架类中添加,不是在视图类中。弹出式对话框中是属于应用程序主框架窗口。)
/////// ///////
二,多线程编程: 1,一些概念:
1)一个进程就是一个运行的程序,进程具有独立的内存、文件句柄和其它系统资源。
2)一个独立的进程可以包含多条执行路径,称为线程。线程有OS管理,每个线程有自己的堆栈。
3)大多数情况下,进程的所有代码和数据空间被进程内所有的线程所共享。 4)Windows提供两中线程:辅助线程 和 用户界面线程。mfc对两种线程都支持。
5)一个用户界面线程有窗口,因而有自己的消息循环;辅助线程没有窗口,因而不需要消息处理。
2,启动辅助线程:
准备工作:写一个全局函数。格式:UINT MyControllingFunction( LPVOID pParam );
启动辅助线程:使用AfxBeginThread函数为全局函数MyControllingFunction创建线程。
说明一:
MFC 通过参数重载提供两个版本的 AfxBeginThread:一个用于用户界面线程,另一个用于辅助线程。
//用户界面线程通常用于处理用户输入和响应用户事件,这些行为独立于执行该应用程序其他部分的线程。已经创建并启动主应用程序线程(在 CWinApp 导出的类中提供)。
//辅助线程通常用于处理后台任务,用户不必等待就可以继续使用应用程序。重新计算和后台打印等任务是很好的辅助线程示例。
说明二:
创建用户界面线程比创建辅助复杂(需要重写一些相关的函数),这里只笔记创建辅助线程一些笔记。
1)AfxBeginThread
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0,
DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
//Return Value:Pointer to the newly created thread object.
//pfnThreadProc:Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows: UINT MyControllingFunction( LPVOID pParam );
//pParam:Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.
//nPriority:The desired priority(优先权) of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, seeSetThreadPriority in the Win32 Programmer‘s Reference.
//nStackSize:Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.
//dwCreateFlags:Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:
[CREATE_SUSPENDED] Start the thread with a suspend count of one. The thread will not execute until ResumeThread is called.
[ 0 ] Start the thread immediately after creation.
//lpSecurityAttrs:Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used.
//Remarks:
Call this function to create a new thread. The first form of AfxBeginThread creates a worker thread. The second form creates a user-interface thread.
To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread. …… 此处隐藏:2411字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [学前教育]MC9S12XS256RMV1 xs128芯片手册4
- [学前教育]安东尼语录经典语录
- [学前教育]e级gps控制测量技术设计书
- [学前教育]苏教版2022-2022学年八年级下学期期末
- [学前教育]装修公司推广 营销
- [学前教育]家政服务合同(完整版)
- [学前教育]湖北省2016届高三联考语文试题
- [学前教育]爱立信无涯学习系统LTE题库1-LTE基础知
- [学前教育]揭秘大众柴油车作弊软件原理
- [学前教育]人才流失原因及对策分析
- [学前教育]房屋建筑施工工程劳务分包合同
- [学前教育]国际贸易实务试卷A卷09.6
- [学前教育]校园废品回收活动计划方案书范文格
- [学前教育]电大成本会计试题及答案
- [学前教育]大学物理实验 华南理工出版社 绪论答案
- [学前教育]爱丁堡产后抑郁量表
- [学前教育]液压冲击的危害、产生原因与防止方法(
- [学前教育]学生工作总结高一学生期中考试总结_020
- [学前教育]人民医院医疗废物管理规章制度大全
- [学前教育]阳光维生素的巨大抗癌潜能阅读题答案.d
- 马云在云锋基金江苏论坛闭幕式的发言
- 试论小学体育教育中的心理健康教育-教
- 语文A版一年级下册《语文乐园一》教学
- 2021四川大学物理化学考研真题经验参考
- [人教A版]2015-2016学年高中数学 第二
- 终端网点销售返利协议书
- 江苏省2015年眼科学主治医师青光眼考试
- 2017年部编人教版八年级语文上册教案
- 十一中学七年级英语上册Unit7Howmuchar
- 以赛促教的创新性实验教学机制建设实践
- 平凉市崆峒区2015七年级下生物期末试题
- 琶洲(地块五)A、B塔楼1、2#塔吊基础
- 一级医院工作制度与人员岗位职责
- 2018北京西城区高三二模理科数学试题及
- 炒股密码线技术 - 图文
- 职高学生生涯发展辅导教案
- 语文人教版四年级上册8 世界地图引出的
- 最新最新人教版二年级上册全册数学教案
- 2017高考英语全国2卷精彩试题(有问题
- 普通心理学笔记




