Sleep in a cycle

Ali Çehreli acehreli at yahoo.com
Fri May 20 15:41:32 UTC 2022


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



More information about the Digitalmars-d-learn mailing list