易截截图软件、单文件、免安装、纯绿色、仅160KB

Delphi多线程学习(10):Label(VCL)同步的问题

上文中,多线程同步主窗体的Label的Caption属性值,发现一个问题:使用Synchronize用于同步的时候,主窗体好像死掉一样;而直接用子程序为Label的引用赋值,则有时会出现“Canvas  does not allow drawing”错误。书上说VCL同步一定要用Synchronize,而不能直接访问。
    测试:
{主窗体}
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm2 = class(TForm)
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

uses TestThread;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
TTestThread.Create(Label1);
//TTestThread.Create(Label2);
//TTestThread.Create(Label3);
end;

end.{多线程类}
unit TestThread;

interface

uses
Classes,StdCtrls;

type
TTestThread = class(TThread)
private
{ Private declarations }
FLabel:TLabel;
Fstr:string;
procedure UpdateLabel;
protected
procedure Execute; override;
public
constructor Create(Lab:TLabel);
end;

implementation

uses Unit2,SysUtils,windows;

{ TTestThread }

constructor TTestThread.Create(Lab: TLabel);
begin
FLabel:=Lab;
Inherited Create(False);
FreeOnTerminate:=True;
end;

procedure TTestThread.Execute;
var
i:Integer;
begin
{ Place thread code here }
for i := 0 to 20000 do
begin
if Not Terminated then
begin
Fstr:=Format('线程ID:%d,第%d个循环',[GetCurrentThreadID,i]);
//UpdateLabel;
Synchronize(UpdateLabel);
end;
end;

end;

procedure TTestThread.UpdateLabel;
begin
FLabel.Caption:=Fstr;
end;

end.
经过测试,只创建一个线程,用Synchronize同步主窗体的一个Label,那么程序无问题,可以见到同步的过程。当�


相关文档:

DELPHI 让窗体不在任务栏显示

需要用到的一个函数:
LONG SetWindowLong(
HWND hWnd,
int nIndex,
LONG dwNewLong
);
其中nIndex GWL_EXSTYLE     Retrieves the extended window styles.
dwNewLong   WS_EX_TOOLWINDOW     Creates a tool window; that is, a window intended to ......

Delphi 资源文件使用


Delphi使用资源文件全攻略
   在通常情况下使用delphi设计程序,都是将字符串、图像等资源直接使用delphi提供的vcl控件加到*.dfm中,这样做会合修改这些资源时带来不便,如果资源被多次引用,这些资源在程序启动时都被加载到内存中,非常耗费系统资源。因此,这就需要一种新的引用资源的文件:资源文件。资源 ......

郁金香delphi外挂编程(翻录版) 种子发布

http://www.91v1.cn/bbs/read.php?tid-1797.html
我们提供的是全套,目前他们发的.社区已录制完毕.一根毛不少.
-------------------------------------------------------------------------------------------------------------------------
该教程由91v1社区免费提供.具体免费领码详情观看种子包,
--------------- ......

Delphi多线程学习(9):多线程数据库查询(ADO)

ADO多线程数据库查询通常会出现3个问题:
1、CoInitialize 没有调用(CoInitialize was not called);所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize。调用CoInitialize失败会产生"CoInitialize was not called"例外。
2、画布不允许绘画(Canvas does not allow drawing);所以,必须通过Synch ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号