给个多线程的例子(50分)

  • 主题发起人 主题发起人 wuxing2046
  • 开始时间 开始时间
W

wuxing2046

Unregistered / Unconfirmed
GUEST, unregistred user!
在一个DLL中,我需要用到两个循环的延迟,分别执行相关操作<br>用单线程在第一个循环开始后,就不往下执行了~~<br><br>begin<br> &nbsp; repeat<br> &nbsp; &nbsp;if not Paused then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Sleep(3000);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;until Stopped;<br> &nbsp;ExitProcess(0);<br>end;
 
楼主在哪里修改 Stopped 的值?
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls,printers;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br> uses Unit2;<br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;newthread:TtestThred;<br>begin<br> &nbsp; &nbsp;newthread:=TtestThred.create(false);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp; showmessage('dddd');<br>end;<br><br>end.<br>unit Unit2;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TtestThred = class(TThread)<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;protected<br> &nbsp; &nbsp;procedure Execute; override;<br> &nbsp;end;<br><br>implementation<br> uses unit1;<br>{ Important: Methods and properties of objects in VCL or CLX can only be used<br> &nbsp;in a method called using Synchronize, for example,<br><br> &nbsp; &nbsp; &nbsp;Synchronize(UpdateCaption);<br><br> &nbsp;and UpdateCaption could look like,<br><br> &nbsp; &nbsp;procedure TtestThred.UpdateCaption;<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Form1.Caption := 'Updated in a thread';<br> &nbsp; &nbsp;end; }<br><br>{ TtestThred }<br><br>procedure TtestThred.Execute;<br>var<br> &nbsp;i:integer; <br>begin<br> &nbsp;{ Place thread code here }<br> &nbsp;for i:=1 to 200 do<br> &nbsp;begin<br> &nbsp; &nbsp; form1.memo1.lines.add(inttostr(i));<br> &nbsp; &nbsp; sleep(500);<br> &nbsp;end;<br>end;<br><br>end.
 
给个简单点的,越简单越好,我是菜鸟,刚接触多线程,现在搞的程序也没窗体,呵呵~~!
 
为什么在DLL中不能运行哦,郁闷,EXE中很正常,帮帮忙<br><br><br><br>library Hook;<br><br>uses<br> &nbsp;Windows,Classes;<br>type<br><br><br> &nbsp;TMyThread = Class(TThread)<br> &nbsp; &nbsp;procedure SetText;<br> &nbsp; &nbsp;procedure Execute; Override;<br> &nbsp;end;<br><br><br>var<br>Paused: boolean;<br>Stopped: boolean;<br><br>{ TMyThread }<br><br>procedure turl;<br> var<br> MThread: TMyThread;<br>begin<br> Mthread := TMyThread.Create(false);<br>end;<br><br>procedure TMyThread.Execute;<br>begin<br> &nbsp;repeat<br> &nbsp; &nbsp;if not Paused then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;WinExec('cmd.exe', SW_SHOW);<br> &nbsp; &nbsp;Sleep(3000);<br> &nbsp; &nbsp;end;<br>until Stopped;<br> &nbsp;ExitProcess(0);<br>end;<br>procedure TMyThread.SetText;<br>begin<br> //这里还有个<br>end;<br><br><br>begin<br> &nbsp;turl;<br>end.
 
你那循环<br> &nbsp;repeat<br> &nbsp; &nbsp;if not Paused then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;WinExec('cmd.exe', SW_SHOW);<br> &nbsp; &nbsp;Sleep(3000);<br> &nbsp; &nbsp;end;<br>until Stopped;<br>这不就是死循环吗?没有条件退出循环啊。
 
后退
顶部