调用一个程序,限定运行时间

来源:百度知道 编辑:UC知道 时间:2024/07/05 23:40:19
运行不完就关掉,运行得完就返回运行时间。
这一点怎么做到?C++

求最简单的方法。
你没搞清楚我的意思。

我的意思是有一个exe程序。
我的程序要去调用它,并且有规定时间,超过这个规定时间没有运行结束的就把它关闭掉。如果在规定时间内结束则获取它运行的时间。

需要定时器。windows平台提供SetTimer。如果不用这个,那就在计算程序中插入计时函数,计算时间再确定是继续计算还是退出。

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
clock_t start, end;
int i,n;
start = clock();
//下面添加程序部分,在下面添加程序,最好是运算复杂点,耗时多一些的

end = clock();
cout<< float(end - start) / CLK_TCK <<endl;
return 0;

}

你可以看看这个演示程序
#include <ctime>
#include <iostream>
int main(void)
{
time_t start,end;
start = time(NULL);
system("pause");//可以用ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOWMAXMIZED)换成记事本等
end = time(NULL);
std::cout<<"程序用时"<<difftime(end,start)<<"秒!\n";//输出时间间隔
system("pause");
return 0;
}