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 @   zjh6  阅读(17)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示