Sleep in a cycle

Alexander Zhirov azhirov1991 at gmail.com
Fri May 20 14:59:07 UTC 2022


I have a loop spinning, I want to pause in it in order to repeat 
the next iteration. An error is displayed during compilation.

```d
import std.stdio;
import modules.monitors; //my module
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;
}

```

Result build:

```sh
$ dub
Performing "debug" build using /usr/bin/ldc2 for x86_64.
mswitch ~master: building configuration "application"...
Running pre-build commands...
source/app.d(32,5): Warning: statement is not reachable
source/app.d(34,5): Warning: statement is not reachable
source/app.d(32,5): Warning: statement is not reachable
source/app.d(34,5): Warning: statement is not reachable
Error: warnings are treated as errors
        Use -wi if you wish to treat warnings only as 
informational.
/usr/bin/ldc2 failed with exit code 1.
```

Is there any way to pause to slow down the cycle?


More information about the Digitalmars-d-learn mailing list