简体   繁体   中英

C# Close Console in WPF application not closing app

I'm making a WPF application, and i made possibility to write log in Console (or .txt file if user wants).

I'm creating Console instance using kernel32.dll's methods: AllocConsole(); FreeConsole(); GetConsoleWindow(); GetConsoleOutputCP(); AllocConsole(); FreeConsole(); GetConsoleWindow(); GetConsoleOutputCP(); .

But if user closes Console my app closes either, but that is not what i want.

I tried using kernel32.dll's SetConsoleCtrlHandler() method or standard WPF events ( AppDomain.CurrentDomain.ProcessExit , App.Current.Exit ) but none of them catches Console closing event.

Is there a way to do this? Or should loggin to Console be implemented by using echo in cmd process?

You can start a process(cmd.exe, with optionally your app or command as parameter). Therefore you can write into the standart input of the console and you will be noticed about the events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM