用C语言解决两个时间的问题···时间!时间啊!

cjh94520 2013-04-14 12:59:18
1.
while(1)
{
dosomething(); //做一些事情
scanf(); //输入一些东西

}


能不能检测到当一定的时间,比如20秒内没有任何输入scanf(),能自动再循环一次。我目的是希望在没有操作输入的时候,能不断重复做我希望做的时间


2.当检测到一定时间,进行某些操作。比如我设置了20秒,当20秒后,进行某些我想要的函数。


真心求大神!!!
...全文
247 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-04-15
  • 打赏
  • 举报
回复
请问楼主一个问题: 20秒到时,恰好输入刚完成,怎么办?
飞天御剑流 2013-04-14
  • 打赏
  • 举报
回复
我晕了,这个破问题还有人要搞多线程。 首先这个问题是无法用标准库函数解决的,因为标准函数都是基于缓冲的,这要求同步I/O,就是必须有输入才会继续下去,那么就得用异步I/O了,这个方向上面已经有人提出来了。 Windows下本来就有一套console模式的输入输出API,既可同步又可异步,客户自行设定,例如readconsoleinput等等,代码不需要举出来了吧?就几行。 Linux下也有类似的API。只是,这些API最好用自己的API封装起来,以利于移植。
庄鱼 2013-04-14
  • 打赏
  • 举报
回复
以BCB为例 class TForm1 : public TForm{ __published: TEdit *In; TTimer *Step; void __fastcall StepTimer(TObject *Sender); void __fastcall InEnter(TObject *Sender); public: void __fastcall ToDo(){} private: AnsiString Message; }; void __fastcall TForm1::InEnter(TObject *Sender) { In->Tag = 1; Message = In->Text; } void __fastcall TForm1::StepTimer(TObject *Sender) { if(In->Tag){ Step->Enabled = false; In->Text = ""; In->Tag = 0; } ToDo(); }
youyou1912 2013-04-14
  • 打赏
  • 举报
回复
额外起一个线程peek即可. 如下(C++代码)

#include <Windows.h>
#include <iostream>
using namespace std;

DWORD WINAPI getCin(void*param)
{
	cout << "getcin...\n";
	while(cin.peek() != EOF);	
	return 1;
}

void test()
{
 			cout << "check buffer...\n";
			SYSTEMTIME x = {};
			GetSystemTime(&x);
			long long start = x.wSecond;
			CreateThread(NULL, 0, getCin, NULL, 0, 0);
			string y;
			while(true)
			{
				GetSystemTime(&x);
				if (x.wSecond >= start + 20)
				{
					start += 20;
					cout << "do something...\n";
				}
				if (cin.rdbuf()->in_avail() != 0)
				{
					std::getline(cin, y);
					cout << "your input: " << y << endl;
				}
			}
}
庄鱼 2013-04-14
  • 打赏
  • 举报
回复
你需要建立一个计时器(或者时钟进程/线程)用于判定定时业务,将数据采集放在一个特定的函数里,当有输入时将改变该函数的状态,终止计时器计数,否则当计时器达到指定时间时将自动触发数据采集操作。
FserSuN 2013-04-14
  • 打赏
  • 举报
回复
问题1 用一个线程检测 问题2 4楼 测试设置VC 6.0 project(工程)->settings(设置)->c/c++ 在Category(分类)中选择:Code generation --多线程

#include <windows.h>
#include <process.h>   
#include <string.h>
#include <stdio.h>
void CheckKey(void *dum);
void doSomething(){printf("doSomething\n");}
char str[1000];
int repeat = 1;
int main()
{
	
    
    while(repeat)
    {
		doSomething();
		_beginthread(CheckKey,0,NULL);
		Sleep(5000L);		
    }
    return 0;
}


void CheckKey(void *dum )
{
    scanf("%s",str);
	if(!strcmp(str,"#"))
		repeat = 0;
	else
		printf("Input Message %s \n",str);
	_endthread(); 
}
qhpxy 2013-04-14
  • 打赏
  • 举报
回复
我觉得大家的方法都在解决停一段时间调用某个函数。而不能解决楼主的一段时间没有scanf()循环体再循环一遍。本人才疏学浅,考略到scanf会阻塞程序的运行,觉得还是的使用多线程解决这个问题。
FserSuN 2013-04-14
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<windows.h>
void doSomeThing(){printf("Do Something\n");}
int main()
{
    int iTickTrigger = 0;
    int iTickCount;
    int delay = 1000*5;
    while(1)
    {
        iTickCount= GetTickCount();
        if(iTickCount>iTickTrigger)
        {
            iTickTrigger = GetTickCount()+delay;
            doSomeThing();
        }
    }
    return 0;
}
漫步者、 2013-04-14
  • 打赏
  • 举报
回复
用time来获取当前时间-last=时间间隔即可判断
mujiok2003 2013-04-14
  • 打赏
  • 举报
回复
C语言本身解决不了,请使用系统的异步IO。
图灵狗 2013-04-14
  • 打赏
  • 举报
回复
用time.h中的time函数即可,它获取的就是绝对的秒数,前后两次的秒数相减就是你所要的时间。

#include <time.h>
int main(void)
{
time_t rawtime;
time(&rawtime);
return 0;
} 

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧