以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  如何用C来实现时间陷阱?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=88568)


--  作者:葛靖青001
--  发布时间:12/7/2010 3:35:00 PM

--  如何用C来实现时间陷阱?
【转自互联网】

时间陷阱.它能是主程序的运行和对事件的处理异步进行.通过设置时间陷阱.可以在规定的时间间隔到达后自动执行某个子程序.子程序运行结束后又回到主程序的断点处继续执行.basic语言提供了有关时间陷阱的语句.可以直接调用.但C语言没有这样的功能,为了能实现上述功能,这里给出了几个子函数以帮助实现.

  #include<dos.h>

  #define Sizeprogram 375

  void interrupt (*oldtimer)();

  void interrupt newtimer();

  static struct SREGS seg;

  unsigned intsp,intss;

  unsigned myss,stack;

  unsigned vseg;

  int running=0;

  int m1,m2,m3=0;

  int (*sub_ptr)();

  void on_timer();

  void timer_on();

  void timer_off();

  void on_timer(t,pp);

  int t;

  int (*pp)();

  {

  sub_ptr=pp;

  m1=t;

  segread(&seg);

  stack=(Sizeprogram-(seg.ds-seg.cs))*16-300;

  myss=_SS;

  oldtimer=getvect(0x1c);

  }

  void timer_on()

  {

  m2=0;

  m3=1;

  setvect(0x1c,newtimer);

  }

  void timer_off()

  {

  m3=0;

  setvect(0x1c,oldtimer);

  }

  void interrupt newtimer()

  {

  (*oldtimer)();

  if(running==0)

  running=1;

  disable();

  intsp=_SP;
intss=_SS;

  _SP=stack;

  _SS=myss;

  enable();

  if(m3==1){

  if(m2==m1*18){

  m2=0;

  (*sb_ptr)();

  }

  m2+=1;

  }

  disable();

  _SP=intsp;

  _SS=intss;

  enable();

  running=0;

  }

  }

  #include<dos.h>

  #define TRUE 1

  void ptr();

  main()

  {

  char ch;

  char *p:

  p=(char *)ptr;

  on_timer(t,p);

  while(TRUE){

  ch=getch();

  switch(ch){

  case ''0'': timer_off();break;

  case ''1'': timer_on();break;

  case ''2'': timer_off();brea;

  default: printf("%c",ch);

  }

  }

  }

  void ptr()

  {

  union REGS in,out;

  in.h.al=''A'';

  in.h.ah=14;

  int86(0x10,&in,&out);

  }


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
13,000.000ms