请教C语言高手

IFIUICIKI 2013-04-12 03:01:14
想要在不断显示倒计时的过程中进行操作,比如输入等。试过多次,没成功。
想问下这个事情能做成吗?网上人说C是单线程,所以不行。
请教下高手,你们怎么看?有什么方法可以实现吗?
...全文
345 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-04-20
  • 打赏
  • 举报
回复
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    SetTimer(NULL,0,1000,myTimerProc1);
    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    return 0;
}
//2012-07-26 17:29:06.375 In main
//2012-07-26 17:29:06.875 In myTimerProc1
//2012-07-26 17:29:07.375 In main
//2012-07-26 17:29:07.875 In myTimerProc2
//2012-07-26 17:29:08.375 In main
//2012-07-26 17:29:08.375 In myTimerProc1
//2012-07-26 17:29:08.875 In main
//2012-07-26 17:29:08.875 In myTimerProc1
//2012-07-26 17:29:09.375 In main
//2012-07-26 17:29:09.890 In myTimerProc2
//2012-07-26 17:29:10.390 In main
//2012-07-26 17:29:10.390 In myTimerProc1
//2012-07-26 17:29:10.890 In main
//2012-07-26 17:29:10.890 In myTimerProc1
//2012-07-26 17:29:11.390 In main
//2012-07-26 17:29:11.890 In myTimerProc2
//2012-07-26 17:29:12.390 In main
//2012-07-26 17:29:12.390 In myTimerProc1
//2012-07-26 17:29:12.890 In main
//2012-07-26 17:29:12.890 In myTimerProc1
//2012-07-26 17:29:13.390 In main
//2012-07-26 17:29:13.890 In myTimerProc2
//2012-07-26 17:29:14.390 In main
//2012-07-26 17:29:14.390 In myTimerProc1
//2012-07-26 17:29:14.890 In main
//2012-07-26 17:29:14.890 In myTimerProc1
//2012-07-26 17:29:15.390 In main
//2012-07-26 17:29:15.890 In myTimerProc2
//2012-07-26 17:29:16.390 In main
//2012-07-26 17:29:16.390 In myTimerProc1
//2012-07-26 17:29:16.890 In main
//2012-07-26 17:29:16.890 In myTimerProc1
//2012-07-26 17:29:17.390 In main
//2012-07-26 17:29:17.890 In myTimerProc2
//2012-07-26 17:29:18.390 In main
//2012-07-26 17:29:18.390 In myTimerProc1
//2012-07-26 17:29:18.890 In main
//2012-07-26 17:29:18.890 In myTimerProc1
//2012-07-26 17:29:19.390 In main
//2012-07-26 17:29:19.890 In myTimerProc2
lm_whales 2013-04-16
  • 打赏
  • 举报
回复
Win7应该是小菜吧,不需要多线程,响应消息即可;显示关机时间用定时器做!
IFIUICIKI 2013-04-16
  • 打赏
  • 举报
回复
引用 5 楼 zhe563323884 的回复:
不用线程也可以的。信号也可以做。最简单的,alarm链触发执行函数
你好,你说的办法太被动了。 我想要的东西你可以看一下8楼。
IFIUICIKI 2013-04-16
  • 打赏
  • 举报
回复
引用 7 楼 buyong 的回复:
是否支持多线程,要看用什么操作系统
win7,64位操作系统。
IFIUICIKI 2013-04-16
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
引用 2 楼 starytx 的回复:c是个编程语言,没有这么说的。使用多线程就可以了,多线程例子一搜一大堆 不用搜:C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626……
大神,太复杂了这个。 这么说吧,我就做了个自动关机,设定好时间后就执行注销计划。这时候程序开始显示秒数倒计时关机,最关键的是我想要在这个倒计时的时候做一件事情,就是按任意键取消关机计划,我对C学的不深,所以一直卡在这里。
buyong 2013-04-15
  • 打赏
  • 举报
回复
是否支持多线程,要看用什么操作系统
zhe563323884 2013-04-15
  • 打赏
  • 举报
回复
不用线程也可以的。信号也可以做。最简单的,alarm链触发执行函数
赵4老师 2013-04-12
  • 打赏
  • 举报
回复
引用 2 楼 starytx 的回复:
c是个编程语言,没有这么说的。使用多线程就可以了,多线程例子一搜一大堆
不用搜:
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <process.h>
#include <io.h>
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
#define ASIZE    200
#define BSIZE    240
#define CSIZE      2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX ;
CRITICAL_SECTION cs_BBB ;
struct FIFO_BUFFER {
    int  head;
    int  tail;
    int  size;
    char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
    int i,j,k;
    char binstr[80];

    Lock(&cs_HEX);
    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%03d %04x -",cn,i);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            Log("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        Log("%s\n",binstr);
    }
    Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;

    lent=0;
    Lock(cs);
    if (fbuf->size>=len) {
        lent=len;
        if (fbuf->head+lent>BSIZE) {
            len1=BSIZE-fbuf->head;
            memcpy(buf     ,fbuf->data+fbuf->head,len1);
            len2=lent-len1;
            memcpy(buf+len1,fbuf->data           ,len2);
            fbuf->head=len2;
        } else {
            memcpy(buf     ,fbuf->data+fbuf->head,lent);
            fbuf->head+=lent;
        }
        fbuf->size-=lent;
    }
    Unlock(cs);
    return lent;
}
void thdB(void *pcn) {
    char        *recv_buf;
    int          recv_nbytes;
    int          cn;
    int          wc;
    int          pb;

    cn=(int)pcn;
    Log("%03d thdB              thread begin...\n",cn);
    while (1) {
        Sleep(10);
        recv_buf=(char *)Cbuf;
        recv_nbytes=CSIZE;
        wc=0;
        while (1) {
            pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
            if (pb) {
                Log("%03d recv %d bytes\n",cn,pb);
                HexDump(cn,recv_buf,pb);
                Sleep(1);
            } else {
                Sleep(1000);
            }
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;

    Lock(cs);
    lent=len;
    if (fbuf->size+lent>BSIZE) {
        lent=BSIZE-fbuf->size;
    }
    if (fbuf->tail+lent>BSIZE) {
        len1=BSIZE-fbuf->tail;
        memcpy(fbuf->data+fbuf->tail,buf     ,len1);
        len2=lent-len1;
        memcpy(fbuf->data           ,buf+len1,len2);
        fbuf->tail=len2;
    } else {
        memcpy(fbuf->data+fbuf->tail,buf     ,lent);
        fbuf->tail+=lent;
    }
    fbuf->size+=lent;
    Unlock(cs);
    return lent;
}
void thdA(void *pcn) {
    char        *send_buf;
    int          send_nbytes;
    int          cn;
    int          wc;
    int           a;
    int          pa;

    cn=(int)pcn;
    Log("%03d thdA              thread begin...\n",cn);
    a=0;
    while (1) {
        Sleep(100);
        memset(Abuf,a,ASIZE);
        a=(a+1)%256;
        if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
        send_buf=(char *)Abuf;
        send_nbytes=ASIZE;
        Log("%03d sending %d bytes\n",cn,send_nbytes);
        HexDump(cn,send_buf,send_nbytes);
        wc=0;
        while (1) {
            pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
            Log("%03d sent %d bytes\n",cn,pa);
            HexDump(cn,send_buf,pa);
            send_buf+=pa;
            send_nbytes-=pa;
            if (send_nbytes<=0) break;//
            Sleep(1000);
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
}
int main() {
    InitializeCriticalSection(&cs_log );
    Log("Start===========================================================\n");
    InitializeCriticalSection(&cs_HEX );
    InitializeCriticalSection(&cs_BBB );

    BBB.head=0;
    BBB.tail=0;
    BBB.size=0;

    _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
    _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);

    if (!access("No_Loop",0)) {
        remove("No_Loop");
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    while (1) {
        Sleep(1000);
        if (No_Loop) break;//
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    Sleep(3000);
    DeleteCriticalSection(&cs_BBB );
    DeleteCriticalSection(&cs_HEX );
    Log("End=============================================================\n");
    DeleteCriticalSection(&cs_log );
    return 0;
}
starytx 2013-04-12
  • 打赏
  • 举报
回复
c是个编程语言,没有这么说的。使用多线程就可以了,多线程例子一搜一大堆
大极张三疯 2013-04-12
  • 打赏
  • 举报
回复
C是可以多线程的,我也初学者,还没学到那里哈,顶一个
一起来玩玩呗 2013-04-12
  • 打赏
  • 举报
回复
炎热的夏季已经过去,家乡想必也要开始落叶了。如果我是小学生,让我以《秋》为题目作文,开头一定是“秋天是收获的季节”。是的,经过了春天的骚动,夏季的彷徨,来到了秋。这是新生报到的季节,动物贮藏食物的季节,马上就到十一长假的季节,这个季节更是收获的季节。 我们在播种时,势必首先考虑收成。种庄稼如此,学习语言更是如此。于是,“语言之争”是各个编程论坛必定讨论的话题,很多甚至是“X经”话题。对此,我的观点是:语言没有好坏之分,只有顺手不顺手和你能发挥到什么程度的问题。我们没有必要追求最新,“最先进”的编程方式,只需要学习好力所能及的语言。 VC的无所不能缺点是繁杂,稍有不慎就不知道什么地方出了包;VB常常被人耻笑过于简单,发布之后要带上一个“仓库”;Delphi虽然有快速开发的优势,但却姥姥不疼舅舅不爱的日渐西山…… 其实对于大多数人来说,我们使用什么应该是需求决定而不是语言本身决定需求,比如:我们希望自动根据数据生成表格,没有必要非用Delphi VC 调用 COM ,我们用用VBA就好了。因此,眼中只有那三种基本结构:判断 循环 跳转就差不多了,再高级一点,弄清楚什么是面向对象面向过程,随便一门语言都不会逃出这点东西。 我很佩服俄罗斯的软件,他们就是将语言工具发挥到极致的例子。比如:我在网上搜索到DOS下硬盘速度测试软件,竟然是Pascal编写的。 至于“学习好力所能及的语言”,我的意思是如果你身边有C语言高手不妨向他请教或者同他切磋;如果你有学好数据结构的理想,不妨买一本Java描述数据结构的图书慢慢啃;如果你有网络数据库+界面的需求不妨考虑一下Delphi…… 因此,我的观点就是“语言没有好坏之分”,关键是你能掌握到什么程度。 当然,如果你能掌握汇编语言,这一切就更不在话下了。我很希望各位能够将学习语言当作乐趣,我们的杂志也将会以如何让读者在阅读中感到快乐感到成就感为目的。

69,377

社区成员

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

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