0
点赞
收藏
分享

微信扫一扫

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()​​,因为​​File​​是​​RAII​​类型,所以不需要它;对象自动在​​析构​​中关闭句柄.

但是,由于代码永远不会到达​​该点​​,因此在​​终止​​程序时可能有​​未刷新​​数据.根据文件系统,在​​writeln​​末尾隐含的​​'\n'​​是否是​​刷新​​触发器.

​3)​​无论如何,我会在​​循环​​中的最后一个​​writeln​​之后添加​​file.flush()​​.

:有什么办法可暂停来​​减慢​​周期?

对的.有更令人愉快语法可以利用D的​​UFCS​​:

Thread.sleep(10.seconds);



举报

相关推荐

0 条评论