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多线程访问,同步问题怎么解决??

解决方案 »

  1.   

    DPR 里加 
     Application.Initialize;  if Application.Handle = 0 then
      Application.CreateHandle;  Application.Run
    或者自己发自定义消息给那个窗口,窗口带自定义的消息处理函数
      

  2.   

    它的宿主进程是IE,不是Delphi编写的程序!
        再等等!!看有没有正确的答案!
      

  3.   

    经过测试,同一个IE窗口下,不同的浏览页面所运行的程序,貌似在共享供一个数据区,但是具体的怎么共享的还不清楚,郁闷Ing!!!哪个高手来指点指点???继续等!!