菜
菜鸟黄
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我做的一个线程,现在问题是线程运行一段时间后会出错提示:"应用程序发生异常 未知的软件异常 (0x0eedfade),位置为 0x7c812a6b".不知道是为什么?unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, ImgList, StdCtrls, ComCtrls;type TForm1 = class(TForm) ProgressBar1: TProgressBar;
Button1: TButton;
Button3: TButton;
ProgressBar2: TProgressBar;
ProgressBar3: TProgressBar;
ProgressBar4: TProgressBar;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ImageList1: TImageList;
Image1: TImage;
Image2: TImage;
Image3: TImage;
Image4: TImage;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
MyNum1: integer;implementation{$R *.dfm}var hTimer1: THandle;{APC 函数}procedure TimerAPCProc1(lpArgToCompletionRoutine: Pointer;
dwTimerLowValue: DWORD;
dwTimerHighValue: DWORD);
stdcall;var Bitmap: TBitmap;
begin
Bitmap :=TBitmap.Create;
try Form1.ProgressBar1.Position :=MyNum1;
Form1.Label1.Caption :=inttostr(MyNum1*10)+'%';
Form1.ImageList1.GetBitmap(MyNum1, Bitmap);
Form1.Image1.Canvas.Lock;
Form1.Image1.Canvas.Draw(0, 0, Bitmap);
Form1.Image1.Canvas.Unlock;
inc(MyNum1);
if MyNum1>10 then
begin
MyNum1 :=0;
end;
except end;
Bitmap.Free;
SleepEx(INFINITE, True);
end;
{线程入口函数}function MyThreadFun1(p: Pointer): Integer;
stdcall;var DueTime: Int64;
begin
DueTime := 0;
{SetWaitableTimer 必须与 SleepEx 在同一线程} if SetWaitableTimer(hTimer1, DueTime, 20, @TimerAPCProc1, nil, False) then
begin
SleepEx(INFINITE, True);
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);var ID: DWORD;
begin
{建立 WaitableTimer 对象} MyNum1 :=0;
if hTimer1 = 0 then
hTimer1 := CreateWaitableTimer(nil, True, nil);
CreateThread(nil, 0, @MyThreadFun1, nil, 0, ID);
{建立线程}end;
procedure TForm1.Button3Click(Sender: TObject);
begin
CancelWaitableTimer(hTimer1);
{取消定时器}end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CancelWaitableTimer(hTimer1);
{取消定时器} CloseHandle(hTimer1);
end;
end.
Button1: TButton;
Button3: TButton;
ProgressBar2: TProgressBar;
ProgressBar3: TProgressBar;
ProgressBar4: TProgressBar;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ImageList1: TImageList;
Image1: TImage;
Image2: TImage;
Image3: TImage;
Image4: TImage;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
MyNum1: integer;implementation{$R *.dfm}var hTimer1: THandle;{APC 函数}procedure TimerAPCProc1(lpArgToCompletionRoutine: Pointer;
dwTimerLowValue: DWORD;
dwTimerHighValue: DWORD);
stdcall;var Bitmap: TBitmap;
begin
Bitmap :=TBitmap.Create;
try Form1.ProgressBar1.Position :=MyNum1;
Form1.Label1.Caption :=inttostr(MyNum1*10)+'%';
Form1.ImageList1.GetBitmap(MyNum1, Bitmap);
Form1.Image1.Canvas.Lock;
Form1.Image1.Canvas.Draw(0, 0, Bitmap);
Form1.Image1.Canvas.Unlock;
inc(MyNum1);
if MyNum1>10 then
begin
MyNum1 :=0;
end;
except end;
Bitmap.Free;
SleepEx(INFINITE, True);
end;
{线程入口函数}function MyThreadFun1(p: Pointer): Integer;
stdcall;var DueTime: Int64;
begin
DueTime := 0;
{SetWaitableTimer 必须与 SleepEx 在同一线程} if SetWaitableTimer(hTimer1, DueTime, 20, @TimerAPCProc1, nil, False) then
begin
SleepEx(INFINITE, True);
end;
Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);var ID: DWORD;
begin
{建立 WaitableTimer 对象} MyNum1 :=0;
if hTimer1 = 0 then
hTimer1 := CreateWaitableTimer(nil, True, nil);
CreateThread(nil, 0, @MyThreadFun1, nil, 0, ID);
{建立线程}end;
procedure TForm1.Button3Click(Sender: TObject);
begin
CancelWaitableTimer(hTimer1);
{取消定时器}end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
CancelWaitableTimer(hTimer1);
{取消定时器} CloseHandle(hTimer1);
end;
end.