各位大虾,,如何在线程中调用线程外的函数,有没有这种可能或变通的方法,,多谢多谢

解决方案 »

  1.   

    procedure Synchronize(Method: TThreadMethod);Executes a method call within the main VCL thread.type TThreadMethod = procedure of object;
    procedure Synchronize(Method: TThreadMethod);DescriptionSynchronize causes the call specified by Method to be executed using the main VCL thread, thereby avoiding multi-thread conflicts. If you are unsure whether a method call is thread-safe, call it from within the main VCL thread by passing it to the Synchronize method.Execution of the thread is suspended while Method is executing in the main VCL thread.Note: You can also protect unsafe methods using critical sections or the multi-read exclusive-write synchronizer.
      

  2.   

    线程外的函数如果不能确定是线程安全的,可使用临界区(TCriticalSection,SyncObjs单元)。
    var
      lockxy:TCriticalSection; //lockxy必须是全局变量,以便对所有线程起作用。
    ... ...LockXY.Acquire; { 获取临界区}
    try
      Y := sin(X); //调用线程外函数
    finally
      LockXY.Release; //{交还临界区}
    end;
      

  3.   

    线程没什么神秘的,和别的Unit单元也没有什么区别,引用了就可以使用里面的函数。唯一要注意的就是同步的问题;
    解决办法如楼上所说...