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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现