关于Windows的进程处理(二)
2017-07-13 00:00
183 查看
最简单例程:
其运行结果为:
我们可以看到进程实际上并未执行够5遍就结束了,这是因为main结束了,系统就结束了所有线程的操作。
这就要求我们在main结束前等待进程的结束,这就需要使用WaitForSingleObject与WaitForMultipleObjects两个状态函数了。
// Thread.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <process.h> unsigned WINAPI ThreadFun1(void *arg) { int cnt = *(int*)arg; for (int i = 0; i < cnt; i++) { Sleep(1000); printf("The thread is running.\n"); } return 0; } int main() { unsigned threadid; int param = 5; HANDLE h = (HANDLE)_beginthreadex(NULL, 0, ThreadFun1, ¶m, 0, &threadid); if (h == 0) printf("Can not create a thread.\n"); else Sleep(3000); printf("End of main.\n"); return 0; }
其运行结果为:
The thread is running. The thread is running. End of main. The thread is running.
我们可以看到进程实际上并未执行够5遍就结束了,这是因为main结束了,系统就结束了所有线程的操作。
这就要求我们在main结束前等待进程的结束,这就需要使用WaitForSingleObject与WaitForMultipleObjects两个状态函数了。
相关文章推荐
- 关于windows的进程处理(八)
- 关于windows的进程处理(五)
- 关于windows的进程处理(七)
- 关于windows的进程处理(四)
- 关于Windows的进程处理(一)
- 关于windows的进程处理(六)
- 关于windows的进程处理(三)
- 关于Windows的SVCHOST.exe进程
- 把windows下的文件拷贝到linux下-关于回车键的处理
- 关于Windows 下查询端口占用情况已经处理方法
- Linux与Windows关于标准输入中涉及EOF的处理方式
- 关于在windows下直接格式化linux后windows的主引导损坏 丢失处理。
- 关于Windowsn 7因验证未通过被视为“非正版”出现“黑屏”的应急处理预案
- 关于杀掉windows进程
- 关于Windows内核对象句柄在进程空间内的存储
- 关于《简明Python教程》中在Windows下编写压缩文件脚本出错的处理
- 关于Windows系统下编程之字符处理
- 关于Windows系统下编程之字符处理
- 关于Windows任务栏不显示的处理方法