以下是一个多线程程序:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
StartGetColor: TButton;
StopGetColor: TButton;
ShowColorLabel: TLabel;
procedure FormCreate(Sender: TObject);
procedure StartGetColorClick(Sender: TObject);
procedure StopGetColorClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
published
procedure ThreadOnTerminate(Sender: TObject);
{ Public declarations }
end;var
Form1: TForm1;
IsViewThreadRun:Boolean;
implementation
uses Unit2;
{$R *.dfm}
var
ViewColorThread:TViewColorThread;procedure TForm1.FormCreate(Sender: TObject);
begin
ViewColorThread:= TViewColorThread.Create;
StartGetColor.Enabled:=True;
StopGetColor.Enabled:=False;
//ViewColorThread.OnTerminate:=Form1.ThreadOnTerminate;
end;procedure TForm1.StartGetColorClick(Sender: TObject);
begin
StartGetColor.Enabled:=not StartGetColor.Enabled;
StopGetColor.Enabled:=not StopGetColor.Enabled;
ViewColorThread.Resume;
end;procedure TForm1.StopGetColorClick(Sender: TObject);
begin
StartGetColor.Enabled:=not StartGetColor.Enabled;
StopGetColor.Enabled:=not StopGetColor.Enabled;
ViewColorThread.Suspend;
end;procedure TForm1.ThreadOnTerminate(Sender:TObject);
begin
Beep;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ViewColorThread.Terminate;
end;end.
//////////////////////////////////////////////////////////////////////////
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit1;type
TViewColorThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
// Procedure OnTerminate;
procedure show;
public
constructor Create;
end;implementation
var
GotColor:Integer;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TViewColorThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TViewColorThread }
constructor TViewColorThread.Create;
begin
inherited Create(True);
//TViewColorThread.OnTerminate:=ThreadOnTerminate;
end;procedure TViewColorThread.Execute;
var
CursorPoint:TPoint;
ScrDC:HDC;
begin
FreeOnTerminate:=True;
while true do
begin
sleep(0);
Synchronize(Show);
ScrDC:=GetDC(0);
GetCursorPos(CursorPoint);
GotColor:=GetPixel(ScrDC,
CursorPoint.x,
CursorPoint.y
);
ReleaseDC(0,ScrDC);
end;
{ Place thread code here }
end;procedure TViewColorThread.show;
begin
Form1.ShowColorLabel.Caption:=IntToStr(GotColor);
end;end.有谁可以帮忙调试一下这个程序??为什么程序在切换线程的过程中以及程序窗口关闭时会不定期的锁死?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
StartGetColor: TButton;
StopGetColor: TButton;
ShowColorLabel: TLabel;
procedure FormCreate(Sender: TObject);
procedure StartGetColorClick(Sender: TObject);
procedure StopGetColorClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
published
procedure ThreadOnTerminate(Sender: TObject);
{ Public declarations }
end;var
Form1: TForm1;
IsViewThreadRun:Boolean;
implementation
uses Unit2;
{$R *.dfm}
var
ViewColorThread:TViewColorThread;procedure TForm1.FormCreate(Sender: TObject);
begin
ViewColorThread:= TViewColorThread.Create;
StartGetColor.Enabled:=True;
StopGetColor.Enabled:=False;
//ViewColorThread.OnTerminate:=Form1.ThreadOnTerminate;
end;procedure TForm1.StartGetColorClick(Sender: TObject);
begin
StartGetColor.Enabled:=not StartGetColor.Enabled;
StopGetColor.Enabled:=not StopGetColor.Enabled;
ViewColorThread.Resume;
end;procedure TForm1.StopGetColorClick(Sender: TObject);
begin
StartGetColor.Enabled:=not StartGetColor.Enabled;
StopGetColor.Enabled:=not StopGetColor.Enabled;
ViewColorThread.Suspend;
end;procedure TForm1.ThreadOnTerminate(Sender:TObject);
begin
Beep;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ViewColorThread.Terminate;
end;end.
//////////////////////////////////////////////////////////////////////////
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Unit1;type
TViewColorThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
// Procedure OnTerminate;
procedure show;
public
constructor Create;
end;implementation
var
GotColor:Integer;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TViewColorThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TViewColorThread }
constructor TViewColorThread.Create;
begin
inherited Create(True);
//TViewColorThread.OnTerminate:=ThreadOnTerminate;
end;procedure TViewColorThread.Execute;
var
CursorPoint:TPoint;
ScrDC:HDC;
begin
FreeOnTerminate:=True;
while true do
begin
sleep(0);
Synchronize(Show);
ScrDC:=GetDC(0);
GetCursorPos(CursorPoint);
GotColor:=GetPixel(ScrDC,
CursorPoint.x,
CursorPoint.y
);
ReleaseDC(0,ScrDC);
end;
{ Place thread code here }
end;procedure TViewColorThread.show;
begin
Form1.ShowColorLabel.Caption:=IntToStr(GotColor);
end;end.有谁可以帮忙调试一下这个程序??为什么程序在切换线程的过程中以及程序窗口关闭时会不定期的锁死?
解决方案 »
- SQL乘积运算~~~
- 数据通信问题
- 请问有谁知道WebBrowser怎么样设置编码格式(encoding)我要设置为GB2312!!!
- 请问如何连接一个端口不是1433的sqlserver2000?(客户端没有Sql server的客户端工具,程序用BDE连接数据库)
- 按理说不会出现这样的情况,但我还是碰到了.
- 高手请进:如何用Delphi+ASP.net来实现Web报表?在线等候!
- 菜鸟问题:Delphi中有没有类switch语句?
- 如何找一个Delhi命令?它的帮助太难找了!
- Midas服务器无法在另一台计算机上运行,只能在本机运行,请问是什么原因?
- ********为什么我在操作EXCEL为了设置某个单元格内容的对齐方式时,总时到 xlCenter 时就过不去?
- tmemo太简单,trich不好用,请问哪里有强大的编辑控件下载(最好有代码和示例的)?
- 一个新手 老菜问题~!关于制作添加,取消,保存,修改等 按钮
private
GotColor:Integer;
...
FreeOnTerminate:=True;
while true do
begin
sleep(100);
begin if Terminated then break; sleep();
end;