level 4
今天写程序时,需要实时汇报系统时间。我想到了多线程。不过总是出错。下面附图。
2015年02月16日 12点02分
1
level 4
过程time是实时汇报时间的。下面主程序就简化为整数加法吧。
只是运行总是不能如愿。我觉得是我对多线程的理解错了,希望有吧友能够帮忙看看怎么解决这类问题。主要是如何在不破坏主程序的情况下,实时汇报时间。
uses dos,windows,crt;
var i,j:integer;
tid:dword;
year,month,date,dow,hour,minute,second,hs:word;
procedure time;
begin
repeat
getdate(year,month,date,dow);
gettime(hour,minute,second,hs);
gotoxy(1,1);
writeln(year,' ',month,' ',date);
gotoxy(1,2);
writeln(hour,' ',minute,' ',second);
delay(1000);
until false;
end;
begin
clrscr;
createthread(nil,0,@time,nil,0,tid);
gotoxy(1,5);
write(1000+100);
end.
2015年02月16日 12点02分
2
源程序比较长就不截图了~
2015年02月16日 12点02分
level 7
Windows XP SP3下由Free Pascal 2.0.4编译运行正常
2015年02月16日 13点02分
3
没有语法错误,但是不能够实时汇报时间,同时进行主程序里的加法。有点纳闷。
2015年02月16日 13点02分
level 9
主程序要死循环,
procedure timethread()
begin
while (true) do begin {死}
{线程中要做的事情}
sleep(1000); {休息1秒再从新循环}
end;
end;
{主程序}
begin
createthread(nil,0,@timethread,nil,0,tid);
while(true)do begin
{主程序中要做的事情;}
sleep(100); {这里要延时,如果不延时cpu会很疯狂}
end;
end.
2015年02月16日 13点02分
4
原来如此,一直在想线程里边已经是死循环了,可就是不行。sleep可以用delay吗?
2015年02月16日 13点02分
回复
��2013
:应该大概可以用delay
2015年02月16日 13点02分
回复
��2013
:主程序中加入退出功能,例如输入 q键退出。主程序如果不循环,主程序一下就执行完了,程序就结束了,程序结束了,其创建的子线程也要结束掉。就像操作系统一样,你的计算机都关机了,你的操作系统创建运行了的一些程序还会运行吗?
2015年02月17日 00点02分
level 4
uses dos,windows,crt;
var i,j:integer;
tid:dword;
year,month,date,dow,hour,minute,second,hs:word;
procedure time;
begin
repeat
getdate(year,month,date,dow);
gettime(hour,minute,second,hs); {输出日期}
gotoxy(1,1);
writeln(year,' ',month,' ',date);
gotoxy(1,2);
writeln(hour,' ',minute,' ',second); {输出时间}
delay(1000);
until false; {死循环}
end;
begin
clrscr;
createthread(nil,0,@time,nil,0,tid);
gotoxy(1,5);
write(1000+100); {简化后的主程序}
end.
2015年02月16日 13点02分
5
level 12
应该是执行完主程序的时间太短了,
lz
没看到吧。。
还有建议lz再弄个停止线程在end前.....(比较习惯而已...)
2015年02月16日 16点02分
6
嗯,上上层楼说的有道理!
2015年02月17日 01点02分
层主,停止或暂停线程有什么函数吗?
2015年02月17日 06点02分
@啤9🍺 这个我也搜过了。编译无法成功。你试过了吗?
2015年02月17日 06点02分
level 9
uses dos,windows,crt;
var i,j:integer;
tid:dword;
year,month,date,dow,hour,minute,second,hs:word;
procedure time;
begin
repeat
getdate(year,month,date,dow);
gettime(hour,minute,second,hs);
gotoxy(1,1);
writeln(year,' ',month,' ',date);
gotoxy(1,2);
writeln(hour,' ',minute,' ',second);
delay(1000);
until false;
end;
begin
clrscr;
createthread(nil,0,@time,nil,0,tid);
gotoxy(1,5);
write(1000+100);
while true do;
end.
2015年02月17日 00点02分
7
这样就解决了 明显没看过我播放音乐的教程贴。
2015年02月17日 00点02分
回复
��2013
:那就是没认真看。
2015年02月17日 01点02分
回复
��2013
:
2015年02月17日 01点02分
level 7
用TerminateThread终止线程的范例如下:
uses dos,windows,crt;
var tid:dword;
year,month,date,dow,hour,minute,second,hs:word;
hthread:hwnd;
procedure time;
begin
repeat
getdate(year,month,date,dow);
gettime(hour,minute,second,hs);
gotoxy(1,1);
writeln(year,' ',month,' ',date);
gotoxy(1,2);
writeln(hour,' ',minute,' ',second);
delay(1000);
until false;
end;
begin
clrscr;
hthread:=createthread(nil,0,@time,nil,0,tid);
gotoxy(1,5);
while not keypressed do;
terminatethread(hthread,0);
while true do;
end.
//程序运行后每秒更新一次时间,直到用户按下一个键
2015年02月17日 08点02分
9
谢谢,很详细。
2015年02月17日 09点02分
回复
��2013
:共同努力,一起进步
2015年02月17日 15点02分