简体   繁体   English

来自Delphi类的同步

[英]Synchronization from class Delphi

I have a question about multithreading in Delphi. 我对Delphi中的多线程有疑问。 Suppose, I have a thread and I have a some class, that do some work and must have synchronization. 假设我有一个线程,并且有一个类,它可以完成一些工作并且必须具有同步性。 How I can make it? 我该怎么做? I make this procedure (In ThreadClass): 我进行以下过程(在ThreadClass中):

procedure TThreadClass.SynchProc(P: TProc);
begin
 ...
 Synchronize(TThreadProcedure(P));
 ...
end;

And I call it from my class, that running in Thread, but ... In procedure symbol "Synchonization" is a method of TThread, that is object "(Self as TThread)", but when I call it proc from my class, variable "Self" doesn't contain a my ThreadClass object (I dont know, that it's contained, may be object of second class, that running in Thread). 我从我的类中调用它,该类在Thread中运行,但是...在过程符号“ Synchonization”中是TThread的一种方法,即对象“(作为TThread的自我)”,但是当我从类中调用proc时,变量“ Self”不包含我的ThreadClass对象(我不知道,它包含的可能是第二类的对象,在Thread中运行)。 respectively that procedure does not work. 分别认为该程序不起作用。 I search oth variants (I'm passed my threadClass object to second class object and try to call "Synchronization" procedure from procedure of second class, but compiller did not want to compile it). 我搜索其他变体(我将我的threadClass对象传递给了第二类对象,并尝试从第二类的过程调用“同步”过程,但是compiller不想对其进行编译)。

Can you help me? 你能帮助我吗? Will be grateful for any help 将不胜感激

with greetings from Ukraine PS Sorry for my bad English 来自乌克兰的问候PS对不起,我的英语不好

I'm not 100% sure I understand, but I think you have a situation like this. 我不确定自己是否100%理解,但我认为您遇到这种情况。 You have a TThread descendent, say TMyThread . 您有一个TThread后代,例如TMyThread And that class in turn uses another class named TThreadClass which does not descend from TThread . 然后该类使用另一个名为TThreadClass类,该类不从TThread TThreadClass You want to call Synchronize from a method of TThreadClass . 您想从TThreadClass的方法调用Synchronize

Here are some options: 以下是一些选项:

  1. Pass the TThread instance to TThreadClass . TThread实例传递给TThreadClass This is a rather brutal solution to the problem. 这是一个相当残酷的解决方案。 Now TThreadClass can do anything to the thread when all it wants to do is call Synchronize . 现在, TThreadClass要调用Synchronize TThreadClass就可以对线程做任何事情。
  2. Pass a procedural variable referring to the Synchronize method to TThreadClass . 将引用Synchronize方法的过程变量传递给TThreadClass This gives TThreadClass the ability to do what it needs and no more. 这使TThreadClass能够执行所需的操作,而不再需要执行其他操作。
  3. Call the TThread.Synchronize class method passing nil for the first parameter. 调用TThread.Synchronize类方法,将第一个参数传递为nil

Of these, the final option is the simplest. 其中,最后的选择是最简单的。 You can do it like this: 您可以这样做:

procedure TThreadClass.SynchProc(P: TThreadProcedure);
begin
  TThread.Synchronize(nil, P);
end;

Note that it is not a good idea to pass in a TProc and cast to TThreadProcedure as per the code in the question. 请注意,按照问题中的代码传入TProcTThreadProcedureTProc并不是一个好主意。 Force the caller to pass in a procedural variable of the right type. 强制调用者传递正确类型的过程变量。 In this case the cast is benign, but you should always aim to avoid casts. 在这种情况下,演员表是良性的,但您应该始终避免演员表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM