Re: Sleep in a cycle

2022-05-22 Thread Alexander Zhirov via Digitalmars-d-learn
My schoolboy mistake. Thank you, 
[Adam](https://forum.dlang.org/post/mbbampewwcrkkltjl...@forum.dlang.org)!


On Saturday, 21 May 2022 at 11:17:04 UTC, Alain De Vos wrote:

Or you could capture a sigint and close the file then.


Yes, exactly, I was thinking in this direction. Probably not 
quite correctly implemented in practice.


Re: Sleep in a cycle

2022-05-21 Thread Alain De Vos via Digitalmars-d-learn

Or you could capture a sigint and close the file then.


Re: Sleep in a cycle

2022-05-20 Thread Ali Çehreli via Digitalmars-d-learn

On 5/20/22 07:59, Alexander Zhirov wrote:
> 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

>  while (true)
>  {

We are in an unconditional loop which is also infinite.

>  }

But you have code after the loop:

>  file.close();
>
>  return 0;
> }
>
> ```

> source/app.d(32,5): Warning: statement is not reachable

That is warning you that your code cannot be executed. One option:

1) Change main's return type to 'void' and remove 'return 0'. (The 
program will automatically return 0 to its starter upon successful 
completion and non-zero upon an uncaught exception.)



2) Remove file.close(), which is not needed because File is an RAII 
type; its objects close their handles in their destructors automatically.


However, since your code never gets to that point, you may have 
unflushed data when you terminate the program. I think it depends on 
your file system whether '\n' which is implied at ends of writeln is a 
flush trigger.


3) Regardless, I would add file.flush() after the last writeln in the loop.

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

That is correct. There is a more pleasant syntax that takes advantage of 
D's UFCS:


  Thread.sleep(10.seconds);

Ali



Re: Sleep in a cycle

2022-05-20 Thread Adam D Ruppe via Digitalmars-d-learn

On Friday, 20 May 2022 at 14:59:07 UTC, Alexander Zhirov wrote:
I have a loop spinning, I want to pause in it in order to 
repeat the next iteration. An error is displayed during 
compilation.


The error has nothing to do with the sleep


source/app.d(32,5): Warning: statement is not reachable
Error: warnings are treated as errors
   Use -wi if you wish to treat warnings only as 
informational.



this is saying the stuff after your `while(true)` is never 
reached because it is an infinite loop.


Sleep in a cycle

2022-05-20 Thread Alexander Zhirov via Digitalmars-d-learn
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?