原文
我想暂停它以重复下一次迭代
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);