相关疑难解决方法(0)

在Delphi-2010中不推荐使用TThread.resume应该使用什么?

在我的多线程应用程序中

我使用TThread.suspendTThread.resume

自从将我的应用程序移至Delphi 2010后,我收到以下交战消息

[DCC警告] xxx.pas(277):不推荐使用W1000符号'Resume'

如果弃用Resume应该使用什么?

编辑1:

我使用Resume命令启动线程 - 因为它创建时将'CreateSuspended'设置为True并在终止线程之前挂起.

编辑2:

这是delphi 2010手册的链接

delphi delphi-2010

35
推荐指数
5
解决办法
3万
查看次数

替代睡在线程内

各种答案表明睡在线程中是一个坏主意,例如:避免睡眠.为什么呢?经常给出的一个原因是,如果正在休眠,很难优雅地退出线程(通过发信号通知它终止).

假设我想定期检查网络文件夹中的新文件,可能每10秒检查一次.这对于优先级设置为低(或最低)的线程来说似乎是完美的,因为我不希望可能耗时的文件I/O影响我的主线程.

有哪些替代方案?代码在Delphi中给出,但同样适用于任何多线程应用程序:

procedure TNetFilesThrd.Execute();
begin
    try
        while (not Terminated) do
            begin
            // Check for new files
            // ...

            // Rest a little before spinning around again
            if (not Terminated) then
                Sleep(TenSeconds);
            end;
    finally
        // Terminated (or exception) so free all resources...
    end;
end;
Run Code Online (Sandbox Code Playgroud)

一个小修改可能是:

// Rest a little before spinning around again
nSleepCounter := 0;
while (not Terminated) and (nSleepCounter < 500) do
    begin
    Sleep(TwentyMilliseconds);
    Inc(nSleepCounter);
    end;
Run Code Online (Sandbox Code Playgroud)

但这仍然涉及睡眠......

c# c++ delphi multithreading

7
推荐指数
1
解决办法
695
查看次数

标签 统计

delphi ×2

c# ×1

c++ ×1

delphi-2010 ×1

multithreading ×1