Thread overview
Interesting Thread.sleep error in Mono-D
Aug 21, 2012
Jeremy DeHaan
Aug 22, 2012
alex
Aug 22, 2012
Jeremy DeHaan
Aug 23, 2012
alex
Aug 24, 2012
Minas Mina
Aug 29, 2012
Marco Leise
August 21, 2012
I was playing around with various parts of D today and I got this very strange error when I call Thread.sleep() in my code in Mono-D.

First, I'll post the code:

module main;

import std.stdio;
import core.thread;

void main(string[] args)
{

	int i = 0;
	
	while (i < 10)
	{
		writeln(i);
		Thread.sleep(dur!("msecs")(500));
		i++;
	
	}
	

    // Lets the user press <Return> before program returns
    stdin.readln();


The funny thing is, the code compiles fine. When I run it via the console(in Windows) it runs as expected. When running it through MonoDevelop/Mono-D, however, I don't see any output until the program stops running and then I see it all at once along with this set of messages.

std.stdio.StdioException@std\stdio.d(2301): Bad file descriptor
----------------
0x00415C1C in char[][] core.sys.windows.stacktrace.StackTrace.trace()
0x00415AA7 in core.sys.windows.stacktrace.StackTrace core.sys.windows.stacktrace.StackTrace.__ctor()
0x0040960B in void std.stdio.StdioException.opCall()
0x004027F4 in uint std.stdio.File.readln!(char).readln(ref char[], dchar) at C:\D\dmd2\src\phobos\std\stdio.d(835)
0x00402770 in immutable(char)[] std.stdio.File.readln!(immutable(char)[]).readln(dchar) at C:\D\dmd2\src\phobos\std\stdio.d(792)
0x00402053 in _Dmain at C:\Users\Jeremy\Documents\Projects\Test2\Test2\main.d(28)
0x004052E4 in extern (C) int rt.dmain2.main(int, char**).void runMain()
0x0040531E in extern (C) int rt.dmain2.main(int, char**).void runAll()
0x00404F40 in main
0x0041D4B5 in mainCRTStartup
0x75D2339A in BaseThreadInitThunk
0x77709EF2 in RtlInitializeExceptionChain
0x77709EC5 in RtlInitializeExceptionChain
----------------
0

1

2

3

4

5

6

7

8

9

The application exited with code: 1



Am I actually doing something wrong? Or is this just something going on with Mono-D?
August 22, 2012
On Tuesday, 21 August 2012 at 07:07:13 UTC, Jeremy DeHaan wrote:
>
> Am I actually doing something wrong? Or is this just something going on with Mono-D?

This is totally normal and not bound to any bug:
It's because MonoDevelop doesn't provide an input stream redirection - so unlike the normal console it supports only text output, not input - you can't type anything in the output panel.
And this is the reason there is such an exception every time you expect some data to be passed to the input stream, like it's done via stdin.readln();

Cheers,
Alex
August 22, 2012
Thanks, Alex! I never noticed that before! Show's you how perceptive I am. Is there any reason the output waits until the end of the program to display any output?

Also, I should really look at my development environments more. I just found the option to have the program run in an external console. That would have solved all my problems.

P.S.
Excellent work with Mono-D! It's the best D IDE I've tried!
August 23, 2012
On Wednesday, 22 August 2012 at 17:43:58 UTC, Jeremy DeHaan wrote:
> Thanks, Alex! I never noticed that before! Show's you how perceptive I am. Is there any reason the output waits until the end of the program to display any output?
>
> Also, I should really look at my development environments more. I just found the option to have the program run in an external console. That would have solved all my problems.
>
> P.S.
> Excellent work with Mono-D! It's the best D IDE I've tried!

Thanks :),

I haven't figured out a way to stop the immediate execution yet.
I also don't know how to make it writing all outputs immediately - sry for that .)
August 24, 2012
On Ubuntu 12.04, dmd 2.060

This works like you wanted - and no exception at the end.


import std.stdio;
import core.thread;

void main(string[] args)
{

	int i = 0;
	
	while (i < 10)
	{
		writeln(i);
		stdout.flush(); // ADDED THIS
		Thread.sleep(dur!("msecs")(500));
		i++;
	
	}
	

    // Lets the user press <Return> before program returns
    stdin.readln();
}
August 29, 2012
Am Fri, 24 Aug 2012 13:00:08 +0200
schrieb "Minas Mina" <minas_mina1990@hotmail.co.uk>:

> On Ubuntu 12.04, dmd 2.060
> 
> This works like you wanted - and no exception at the end.
> 
> 		writeln(i);
> 		stdout.flush(); // ADDED THIS

I use writeln of stdout, which has an implicit flush():

		stdout.writeln(i);

-- 
Marco