Eri*_*c G 1 delphi asynchronous delphi-2010
我有一个运行长操作的模型对象.我正在尝试在线程中运行其中一些操作以保持接口响应,一次下载2件事等,但我想尽可能地从接口代码中隐藏这些细节.我正在尝试使用AsyncCall库,但有问题.
type EUpdaterAction = (auFoundCurrentVersion, auFoundUpdateVersion);
type
TUpdater = class
public
procedure loadCurrentVersion();
procedure notify(action: EUpdaterAction);
end;
procedure TUpdater.loadCurrentVersion();
begin
TAsyncCalls.Invoke(procedure
begin
Assert(Windows.GetCurrentThreadId() <> System.MainThreadID);
//Really long code
TAsyncCalls.VCLSync(procedure begin notify(auFoundCurrentVersion); end);
end);
end;
Run Code Online (Sandbox Code Playgroud)
您需要调用IAsyncCall.ForceDifferentThread以强制代码在不同的线程中运行.在您的示例中,TAsyncCalls.Invoke()返回的接口会立即释放,因为该函数结束并且释放IAsyncCall接口会调用该Sync方法.并且由于您的任务尚未启动,该Sync方法将在同一个线程中执行它,除非ForceDifferentThread调用该方法.
TAsyncCalls.Invoke(procedure
begin
Assert(Windows.GetCurrentThreadId() <> System.MainThreadID);
//Really long code
TAsyncCalls.VCLSync(procedure begin notify(auFoundCurrentVersion); end);
end).ForceDifferentThread; // <<<<<
Run Code Online (Sandbox Code Playgroud)
但这真的是你想要的吗?我想你想在loadCurrentVersion()退出后保持线程活着.而对于这个AsyncCalls不是正确的工具.
| 归档时间: |
|
| 查看次数: |
1041 次 |
| 最近记录: |