我在线程里读注册表。在外面显示的时候值怎么总是空?
高手救命!!!
unit readreg;interfaceuses
  Classes,Registry,Windows,  Messages, SysUtils, Variants,  Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls,StrUtils;type
  Treadreg = class(TThread)
  private
    { Private declarations }  protected
    procedure Execute; override;    public
        temp_result_string:string;
  end;
  const
  RegRoot: Cardinal = HKEY_CURRENT_USER;
  RegKeyPath = 'Software\Classes\CLSID\{CD546EDB-A86C-44FE-8C95-6C8E97DB9707}';
  RegEntryName = 'LastPostion'; // intended misspelling for easier searchingimplementation{ 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 readreg.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ readreg }procedure Treadreg.Execute;
begin
  with TRegistry.Create do
  try
    RootKey:=RegRoot;
    if OpenKey(RegKeyPath,true) then  temp_result_string:=ReadString(RegEntryName);  finally
    Free;
  end;
end;
end.
/////////////////////////////////////////////////
procedure TForm1.Button4Click(Sender: TObject);
var
result_string:string;
Tresult_str:Treadreg;
beginTresult_str:= Treadreg.Create(true);
Tresult_str.FreeOnTerminate:=true;result_string:=Tresult_str.temp_result_string;       showmessage(result_string);
end;我在线程里读注册表。在外面显示的时候值怎么总是空?

解决方案 »

  1.   

    调了一会,偶也奇怪了,很长时间不做多线程了,还是考虑做个显示函数,用
    Synchronize引出到主线程中执行吧.
      

  2.   

    lws0472(hero)非常感谢了。
    不过具体应该应该怎么做?
      

  3.   

    你具体的想做什么,是不是要在创建的线程中读取注册表,然后在主线程中访问啊。
      你是不是只是测试程序,实际应用中根本没有必要。
      我也解释不清上面的问题,可能是你创建的线程级别太低,还没分到时间片,变量还没有赋值,主线程就已经showmessage了,可以考虑将线程的级别设的高点,也可以用Synchronize引出个过程,放在主线程的showmessage之前执行。你手上应该有资料的,资料中肯定讲到Synchronize的使用.在过程中读注册表值。
      

  4.   

    type
      Treadreg = class(TThread)
      private
        { Private declarations }
         procedure ShowValue;//声明一个显示值的方法
      protected
        procedure Execute; override;    public
            temp_result_string:string;
      end;procedure TReadReg.ShowValue;
    begin
      //这里在主线程显示temp_result_string的值
      //如;Form1.Edit1.text:=temp_result_String;end;
    procedure Treadreg.Execute;
    begin
      with TRegistry.Create do
      try
        RootKey:=RegRoot;
        if OpenKey(RegKeyPath,true) then  temp_result_string:=ReadString(RegEntryName);  finally
        Free;
      end;
      Synchronize(ShowValue);//将该线程的方法传到主线程执行显示
    end;
      

  5.   

    从程序中看,你的Execute还没有执行,因为你创建时是阻塞的,又没有调用Resume。
      

  6.   

    Tresult_str:= Treadreg.Create(false);
      

  7.   

    hglcsdn(中文hgl)说的是对的啊!线程创建但还未启动!
      

  8.   

    楼上的都说完了
    启动使用  Tresult_str.Execute
    就可以了
      

  9.   

    maozhuxiwansui谢谢了。
    可是我Tresult_str.Execute;不行啊。
    [Error] Unit1.pas(134): Undeclared identifier: 'Execute'
    是为什么?
      

  10.   

    线程创建的时候就自动执行了,怎么还需要execute,郁闷
      

  11.   

    对啊。我就是不明白了。我create的时候难道没执行吗?为什么会这样?
      

  12.   

    create(createsuspended:boolean)
    如果参数为true的话(创建即被挂起),你要显式的声明resume线程才开始执行。
    如果参数为false的话(创建即被运行),就直接运行了。