DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Fri Dec 28, 2007 9:39 am Post subject: [Delphi]Suspending A Thread (class(TThread)) |
|
|
While i had no internet (connection problem with ISP) i didn't know how to use threads, so i opend up delphi's 7e help topic and searched for "TThread", i found many intresting topics about it, well i just wanted to know how to declare a thread class and use it, and i did (i memorized some of db's code too), anyways when i'm suspending a thread i get an access violation error, why ?
my code:
| Code: | unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyThread = class(TThread)
private
{Private Dec}
protected
Procedure RunApp;
Procedure Increase;
Procedure Exit;
end;
type
TMyThreadTwo = class(TThread)
private
{Private Dec}
protected
Procedure Suspendit;
Procedure Resumeit;
End;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyThread: TMyThread;
MyThreadTwo: TMyThreadTwo;
implementation
{$R *.dfm}
Procedure TMyThreadTwo.Resumeit;
begin
MyThread.Resume;
End;
Procedure TMyThreadTwo.Suspendit;
begin
MyThread.Suspend;
end;
Procedure TMyThread.Increase;
var i:integer;
begin
for i:=1 to 90 do
begin
Form1.Edit1.Text:=IntToStr(StrToInt(Form1.Edit1.Text)+1);
End;
end;
Procedure TMyThread.Exit;
begin
application.Terminate;
end;
Procedure TMyThread.RunApp;
var
X:integer;Y:integer;
begin
Y:=StrToInt(Form1.Edit1.Text);
for X:=1 to Y do begin ShowMessage('Hi');
if ((X mod 4)= 0) then MyThread.Suspend; //ShowMessage(IntToStr(X));
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MyThread.RunApp;
//MyThread.Increase;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
{MyThreadTwo.Suspendit;}
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MyThreadTwo.Resumeit;
end;
end. |
|
|