0
我用C++编写了一个程序来完成某些任务。用户选择将完成哪些。首先,程序会对用户选择的全部内容进行处理,然后等待用户指定的时间(以秒为单位)再次执行某个任务;任务可以有不同的等待时间。定时器不能正常工作的C++程序
在我的电脑里它工作的很好,但是我把可执行文件给了某人,他告诉我他第一次做了他选择的任务(他只是选择了任务3),但是在等待时间之后结束了,它什么都没做。我不知道问题的根源,它与我使用的相同的.exe文件,在我的电脑中工作正常,行为应该是相同的,但由于某种原因,它不是。
我使用了msvc 2012.而且,用户拥有Visual C++ Redistributable。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
int GetLowestWaitingTime(int* a)
int main()
{
int WaitingTime[3];
int TimeForTask[3];
bool task[3];
int i;
cout<<"1=yes 0=no. Press enter after you have typed the chosen number" <<endl;
for(i = 0; i < 3;i++){
cout<<"Do you want task " <<i+1 <<" to be performed? ";
cin >> task[i];
if(task[i])
{
cout<<"Enter the waiting time for task " <<i+1 <<" in seconds: ";
cin >> TimeForTask[i];
}else{
TimeForTask[i] = -1;
}
WaitingTime[i] = 0;
}
while(1)
{
for(i = 0; i < 3; i++){
if(task[i] && WaitingTime[i] == 0)
{
//Do task i+1
WaitingTime[i] = TimeForTask[i];
}
}
SleepTime = GetLowestWaitingTime(WaitingTime);
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2); //turn off monitor
Sleep(SleepTime*1000);
for(i = 0; i < 3; i++)
{
if(WaitingTime[i] != -1){
WaitingTime[i] -= SleepTime;
}
}
}
return 0;
}
int GetLowestWaitingTime(int* a)
{
int l = -2;
if(a[0] != -1 && ((a[0] < a[1]) || (a[1] == -1))){
l = a[0];
}else if(a[1] != -1){
l = a[1];
}
if(l == -2){l = a[2];}else if(l > a[2] && a[2] != -1){l = a[2];}
return l;
}
你有一段时间(1),你似乎没有打出来的。 SendMessage()是否这样做?否则,你将永远停留在while循环中。 – wolfPack88 2013-03-06 21:07:52
它可能挂在那个SendMessage上。我不认为它真的做你打算做的事情。 – 2013-03-06 21:51:43