我们用到多很多函数都可以设置超时,但我现在就是想弄明白,象这样的函数到底该怎么写!
解决方案 »
- 创建mschart失败 求指教啊
- VC++6.0,用了manifest后,显示win7效果按钮边缘不好看,如图
- 是否加入兔子党,犹豫中,散分吧!
- 插入特殊符号%的问题
- SetItemState的最后一个参数是什么意思?
- 那儿有英特尔的C++编译器下载?
- 在access数据库中如果想查找有多少个重复的记录,怎么写sql语句?
- 怎么样使用BoundsChecker~~~~
- 我在客户区画了一个矩形,但激活其他窗口再切换回来,却没有了,请问怎么样才能使其不被重画,请问用什么API?谢谢!
- 从学VB过度到VC时遇到的问题。
- 各位高手,有python语言写的脚本文件,的源程序文件怎么生成项目方案?
- 求助,关于将图片复制到剪贴板中的问题!!
那用waitsingleobject()吧,等待一个对像,并settimer,指定时间内还没等到,就在ontimer里把等待对象的状态置为信号状态。然后就可以继续执行下去了
DoSomething();
dwTimeElapsed = GetTickCount() – dwOldTime;
#include <windows.h>
#include <iostream.h>void SomeFunction(DWORD dwTime)
{
DWORD dwOldTime = GetTickCount();
while(GetTickCount() - dwOldTime < dwTime)
{
cout<<"I'm running."<<endl;
}
cout<<"Exit."<<endl;
}int main()
{
SomeFunction(1000);//1秒后退出
return 0;
}
同5楼#include <windows.h>
#include <iostream.h>void SomeFunction(DWORD dwTime)
{
DWORD dwOldTime = GetTickCount();
while(GetTickCount() - dwOldTime < dwTime)
{
cout<<"I'm running."<<endl;
}
cout<<"Exit."<<endl;
}int main()
{
SomeFunction(1000);//1秒后退出
return 0;
}
bool funA
{
if(时间>5s&&funB还未返回值)
return flase
}这样的超时函数怎么弄呢 funB是调用第三方dll的
bool funA
{
if(时间>5s&&funB还未返回值)
return flase
}这样的超时函数怎么弄呢 funB是调用第三方dll的
首先调用FunB修改为用多线程调用,FunA内等待他返回,如果等待超时还没返回,就强制结束这个线程,然后FunA返回false即可。仅供参考。
void ThreadFunc()//线程函数
{
funB();
}int funA()
{
HANDLE hThread;
DWORD ThreadID;
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);
DWORD hResult = WaitForSingleObject(hThread, 5000);//等待5秒
if(hResult == WAIT_OBJECT_0)
{
//调用成功
}
else if (hResult == WAIT_TIMEOUT)
{
//超时
}
else
{
//出错
}}