Calling Console.WriteLine before allocating the console

前端 未结 3 1615
不思量自难忘°
不思量自难忘° 2021-01-12 12:29

I\'ve recently encountered the following problem with my application: it didn\'t show any console output, though the console had been allocated by using AllocConsole

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 13:16

    The first time you use Console.WriteLine, the Console class creates a TextWriter and associates it with the Console.Out property. The way it does this is to use Win32 to open the low-level file handle associated with the standard output file handle. If the standard output handle is invalid, Console.Out is set to TextWriter.Null, which discards all output.

    The Win32 AllocConsole function, creates and sets the standard output handle so after calling it the standard output handle is either different or now valid. In either case, Console.Out has already been set either to use the old standard output or to discard all output.

    To force a re-open of Console.Out after calling AllocConsole, you can use this method:

    • Console.OpenStandardOutput

提交回复
热议问题