H
hotboy
Unregistered / Unconfirmed
GUEST, unregistred user!
最近在学习多线程编程。<br>我在一个窗体上放下Edit1,和Button1<br>还有Edit2,和Button2。<br>设想:<br>点击Button1启动一个线程,在Edit1不停的变动数字。<br>点击Button2启动第二个线程,在Edit2不停的变动数字。<br><br>可是在点击完Button1,后在点击Button2 会死机。为什么啊?<br>对多线程不太理解,请大师指导。<br>我的代码如下:<br>program Project1;<br><br>uses<br> Forms,<br> Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br><br>begin<br> Application.Initialize;<br> Application.CreateForm(TForm1, Form1);<br> Application.Run;<br>end.<br><br>-------------<br>unit Unit1;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> Button2: TButton;<br> Edit2: TEdit;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br>var<br> Form1: TForm1;<br>implementation<br>uses Calculate,Calculate2;<br>{$R *.dfm}<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>TMyThread.Create(False);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>TMyThread2.Create(False);<br>end;<br>end.<br>-------------<br>unit Calculate;<br><br>interface<br><br>uses<br> Classes,SysUtils;<br><br>type<br> TMyThread = class(TThread)<br> private<br> { Private declarations }<br><br> protected<br> procedure Execute; override;<br> end;<br><br>implementation<br> uses Unit1;<br><br> { TMyThread }<br><br>procedure TMyThread.Execute;<br> var i:Integer;<br>begin<br> { Place thread code here }<br> for i:=1 to 1000000000 do<br> Form1.Edit1.Text:=IntToStr(i);<br><br>end;<br><br>end.<br> ------------<br>unit Calculate2;<br><br>interface<br><br>uses<br> Classes,SysUtils;<br><br>type<br> TMyThread2 = class(TThread)<br> private<br> { Private declarations }<br> protected<br> procedure Execute; override;<br> end;<br><br>implementation<br>uses unit1;<br><br>{ TMyThread2 }<br><br>procedure TMyThread2.Execute;<br>var i:Integer;<br>begin<br> { Place thread code here }<br> for i:=1 to 1000000000 do<br> Form1.Edit2.Text:=IntToStr(i);<br>end;<br><br>end.