ActiveXForm中有个子线程(TMyShowThread)它的功能就是简单的更改主Form上的可视化控件的显示状态(1.Caption,2.进度条),但是子线程(TMyShowThread)在更改主Form的VCL控件的时候,怎么才能控制其同步问题??,以下为主Form的关键代码及线程单元的所有代码,synchronize在这里起作用!//=======================================================================================================
//================================ActiveFormThreadTestImpl 单元==========================================
//=======================================================================================================unit ActiveFormThreadTestImpl;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActiveX, AxCtrls, ActiveFormThreadTestProj_TLB, StdVcl, ExtCtrls,
RzStatus, StdCtrls, RzLabel;type
TActiveForm2 = class(TActiveForm, IActiveFormThreadTest)
Panel2: TPanel;
lbl_Main: TRzLabel;
Panel1: TPanel;
UnZipPercentLab: TRzLabel;
UnZipPercentBar: TRzProgressStatus;
ThreadContronlTimer: TTimer;
Button1: TButton;
procedure ThreadContronlTimerTimer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
//此部分省略.....
protected
{ Protected declarations }
//上边部分省略
function Get_TestFileName: WideString; safecall;
procedure Set_TestFileName(const Value: WideString); safecall;
public
{ Public declarations }
Num1:Integer;
sFileURL:String;
isCanRun:Boolean;
procedure Initialize; override;
end;implementationuses ComObj, ComServ, MyShowThread;{$R *.DFM}{ TActiveFormThreadTest }
//上边部分省略...
function TActiveForm2.Get_TestFileName: WideString;
begin
Result:=sFileURL;
end;procedure TActiveForm2.Set_TestFileName(const Value: WideString);
begin
//程序入口
sFileURL:=Value;
isCanRun:=False;
ThreadContronlTimer.Enabled:=True;
end;procedure TActiveForm2.ThreadContronlTimerTimer(Sender: TObject);
var
myShowThread:TMyShowThread;//线程类
begin
//
ThreadContronlTimer.Enabled:=False;
if isCanRun then begin
//子线程结束后,进入这里执行,
//此部分省略......
end else begin
lbl_Main.Caption:='下载子线程 准备执行.......';Sleep(1000);
myShowThread:=TMyShowThread.Create(Self);//创建子线程,用于更改VCL控件的状态
myShowThread.Resume;
end;
end;initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
TActiveForm2,
Class_ActiveForm2,
1,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmSingle);
end.//=======================================================================================================
//=========================================MyShowThread 单元=============================================
//=======================================================================================================Unit MyShowThread;interface
uses
Windows,Classes,Dialogs,ActiveFormThreadTestImpl;
type
TMyShowThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(aActiveForm:TActiveForm2);
procedure BeginDo;
end;
implementation
var
MainForm:TActiveForm2;constructor TMyShowThread.Create(aActiveForm:TActiveForm2);
begin
inherited Create(True);
FreeOnTerminate := true;
//
MainForm:=aActiveForm;
end;
procedure TmyShowThread.BeginDo;
begin
MainForm.Num1:=2;
MainForm.lbl_Main.Caption:='下载子线程 正在执行.......';
MainForm.UnZipPercentLab.Caption:='开始下载,请稍后.......';
MainForm.UnZipPercentBar.Percent:=1;
Sleep(3000); MainForm.Num1:=3;
MainForm.UnZipPercentBar.Percent:=30;
Sleep(3000); MainForm.Num1:=4;
MainForm.UnZipPercentBar.Percent:=60;
Sleep(3000); MainForm.Num1:=5;
MainForm.UnZipPercentBar.Percent:=90;
Sleep(3000); MainForm.Num1:=6;
MainForm.UnZipPercentLab.Caption:='下载完毕';
MainForm.UnZipPercentBar.Percent:=100;
end;
procedure TMyShowThread.Execute;
begin
BeginDo;//此为更改MainForm中VCL控件的方法,在此线程中如何控制同步问题,如果我要有N个Thread呢?
//Synchronize(BeginDo);不行!
MainForm.isCanRun:=True;
MainForm.ThreadContronlTimer.Enabled:=True;
end;
end.希望大家帮忙给看一下,我弄了好久了,看了很多,大富翁上的帖子也看了,但是看不明白,也没个例子!郁闷!有例子理解的还快点!ActiveForm中VCL多线程访问,同步问题怎么解决??
//================================ActiveFormThreadTestImpl 单元==========================================
//=======================================================================================================unit ActiveFormThreadTestImpl;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ActiveX, AxCtrls, ActiveFormThreadTestProj_TLB, StdVcl, ExtCtrls,
RzStatus, StdCtrls, RzLabel;type
TActiveForm2 = class(TActiveForm, IActiveFormThreadTest)
Panel2: TPanel;
lbl_Main: TRzLabel;
Panel1: TPanel;
UnZipPercentLab: TRzLabel;
UnZipPercentBar: TRzProgressStatus;
ThreadContronlTimer: TTimer;
Button1: TButton;
procedure ThreadContronlTimerTimer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
//此部分省略.....
protected
{ Protected declarations }
//上边部分省略
function Get_TestFileName: WideString; safecall;
procedure Set_TestFileName(const Value: WideString); safecall;
public
{ Public declarations }
Num1:Integer;
sFileURL:String;
isCanRun:Boolean;
procedure Initialize; override;
end;implementationuses ComObj, ComServ, MyShowThread;{$R *.DFM}{ TActiveFormThreadTest }
//上边部分省略...
function TActiveForm2.Get_TestFileName: WideString;
begin
Result:=sFileURL;
end;procedure TActiveForm2.Set_TestFileName(const Value: WideString);
begin
//程序入口
sFileURL:=Value;
isCanRun:=False;
ThreadContronlTimer.Enabled:=True;
end;procedure TActiveForm2.ThreadContronlTimerTimer(Sender: TObject);
var
myShowThread:TMyShowThread;//线程类
begin
//
ThreadContronlTimer.Enabled:=False;
if isCanRun then begin
//子线程结束后,进入这里执行,
//此部分省略......
end else begin
lbl_Main.Caption:='下载子线程 准备执行.......';Sleep(1000);
myShowThread:=TMyShowThread.Create(Self);//创建子线程,用于更改VCL控件的状态
myShowThread.Resume;
end;
end;initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
TActiveForm2,
Class_ActiveForm2,
1,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmSingle);
end.//=======================================================================================================
//=========================================MyShowThread 单元=============================================
//=======================================================================================================Unit MyShowThread;interface
uses
Windows,Classes,Dialogs,ActiveFormThreadTestImpl;
type
TMyShowThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(aActiveForm:TActiveForm2);
procedure BeginDo;
end;
implementation
var
MainForm:TActiveForm2;constructor TMyShowThread.Create(aActiveForm:TActiveForm2);
begin
inherited Create(True);
FreeOnTerminate := true;
//
MainForm:=aActiveForm;
end;
procedure TmyShowThread.BeginDo;
begin
MainForm.Num1:=2;
MainForm.lbl_Main.Caption:='下载子线程 正在执行.......';
MainForm.UnZipPercentLab.Caption:='开始下载,请稍后.......';
MainForm.UnZipPercentBar.Percent:=1;
Sleep(3000); MainForm.Num1:=3;
MainForm.UnZipPercentBar.Percent:=30;
Sleep(3000); MainForm.Num1:=4;
MainForm.UnZipPercentBar.Percent:=60;
Sleep(3000); MainForm.Num1:=5;
MainForm.UnZipPercentBar.Percent:=90;
Sleep(3000); MainForm.Num1:=6;
MainForm.UnZipPercentLab.Caption:='下载完毕';
MainForm.UnZipPercentBar.Percent:=100;
end;
procedure TMyShowThread.Execute;
begin
BeginDo;//此为更改MainForm中VCL控件的方法,在此线程中如何控制同步问题,如果我要有N个Thread呢?
//Synchronize(BeginDo);不行!
MainForm.isCanRun:=True;
MainForm.ThreadContronlTimer.Enabled:=True;
end;
end.希望大家帮忙给看一下,我弄了好久了,看了很多,大富翁上的帖子也看了,但是看不明白,也没个例子!郁闷!有例子理解的还快点!ActiveForm中VCL多线程访问,同步问题怎么解决??
解决方案 »
- 求解决方案:一个程序,想获得用户手机发送过来的信息,怎么实现?不用串口,不用手机猫
- 谁能用delphi来实现金山词霸在线版的googledesktop2插件
- 100分。在线等待,关于
- 请教一个有关dbgrideh 的问题,我想了很久,都解决不了,请大家帮帮我吧!!!
- 如何把工程project1中的unit1 加入到工程project2 里???
- 如何个listview最后一行最后一列付值
- 关于coolbar的问题
- 关于MDI中的子窗体调用出错的问题?....
- 在Delphi开发的应用程序中,有时汉字会变成乱码,怎么回事?如何解决?
- 在线调查:这里有多少人是三角高手?
- 定时显示已用时间和剩余时间:WaitableTimer和线程封装的问题
- bitblt多次调用后会提示系统资源不足,该如何解决?
Application.Initialize; if Application.Handle = 0 then
Application.CreateHandle; Application.Run
或者自己发自定义消息给那个窗口,窗口带自定义的消息处理函数
再等等!!看有没有正确的答案!