d暂停循环

原文
我想暂停它以重复下一次迭代

import std.stdio;
import modules.monitors; //我的模块
import core.thread;

int main(string[] args)
{
    string path = "mswitch.log";
    if (args.length > 1)
    {
        path = args[1];
    }
    auto file = File(path, "w");

    auto monitors = getMonitorsInfo();
    file.writeln(monitors);

    while (true)
    {
        setPrimaryMonitor(monitors[1].name);
        file.writeln("-- Switch monitors --");
        swapMonitors(monitors[0].name, monitors[1].name, Relation.right_of);
        monitors = getMonitorsInfo();
        file.writeln(monitors);

        Thread.sleep(dur!("seconds")(10));
    }

    file.close();

    return 0;
}

警告无法执行代码.一种选择:
1)更改main的返回类型为'void'并删除'return0'.(程序在成功完成时自动返回0到它启动器,在未捕获异常时自动返回非零.)
2)去掉file.close(),因为FileRAII类型,所以不需要它;对象自动在析构中关闭句柄.
但是,由于代码永远不会到达该点,因此在终止程序时可能有未刷新数据.根据文件系统,在writeln末尾隐含的'\n'是否是刷新触发器.
3)无论如何,我会在循环中的最后一个writeln之后添加file.flush().
:有什么办法可暂停来减慢周期?
对的.有更令人愉快语法可以利用D的UFCS:

 Thread.sleep(10.seconds);
posted @ 2022-05-21 16:07  zjh6  阅读(21)  评论(0)    收藏  举报  来源