请问在Delphi中我如果主线程中有个方法要同步访问(每次只允许一个线程访问)。有没有类似于java或者C中的Synchronize关键字作用的东西,让凡是访问这个方法的线程都是自动同步访问。用delphi的Synchronize好像必须在thread类中定义个方法,用这个方法去调用主程序的需要同步的方法,再用Synchronize(线程中的方法)方式调用。这样非常麻烦,尤其是线程中的方法好像还没法代参数。不知道有没有好的方法解决,谢谢!

解决方案 »

  1.   

    在Delphi中也是用
    Synchronize同步的
    就是这样,不能带参数,需要另外写个不带参数的过程,调用你要的方法
      

  2.   

    对于VCL,这个方法是没有选择的余地的,你只能如此
      

  3.   

    可以在thread中自己写一个函数,调用form中有参数的函数,然后在线程的execute中用synchronize()调用该函数。
      

  4.   

    procedure TRefreshThread.Execute;
    begin
      { Place thread code here }
      FreeOnTerminate := True;
      Priority :=  tpLower ;//       tpNormal;
      while not (Terminated or Application.Terminated) do
      begin
        if HintForm=nil then
          Terminate;
        Sleep(100);//Below's is what you need
        Synchronize(Application.ProcessMessages);
        //Synchronize(Application.HandleMessage);
        Synchronize(MyPro);
      end;
    end;