2

Quick note for reviewers: Since my question was randomly closed and marked as an "exact" duplicate, I was forced unfortunately to make this question again. Please actually read the problem before randomly marking and closing the question as an exact duplicate of a WinForms issue in which the solution does not work for me. Especially an out of date answer that I wouldn't get a response to if I needed clarification anyway. I'll be happy to add some clarification if necessary. :)

I just want my question the ability to be actually answered by other users, don't just needlessly close my question because you think an answer that works for an entirely different application structure, works. Make sure that the answer that you provide actually works before closing.


I feel as though I'm missing something extremely simple/stupid here so forgive me if this is a dumb question. To summarize:

I created a Windows Application. All of the stuff in the application works as intended. This Windows Application however can also utilize the command line interface by accepting switches which will then invoke some of of the methodology within the application. I got that working as intended as well.

So let's say for example I run the application in the command line like MyApp.exe -help. The application will output the results to the Console as expected. However, whenever I write to the console, it always waits for me to press Enter before it actually returns to the state in which I can run other commands. i.e.

C:\Users\user\Desktop>_

The extremely stripped down version of the code, for visual purposes would be:

public partial class App : Application
{
  protected override void OnStartup(StartupEventArgs e)
  {
    base.OnStartup(e);

    if(e.Args.Length > 0)
    {
      AttachConsole(-1);
      Console.WriteLine("Pretend this a helpful message");
    }
    else
    {
      new MainWindow().ShowDialog();
    }
    Shutdown();
  }
}

How can I make it not do this?

This isn't a showstopper or anything, it's just something that's annoying me and would be nice to resolve. I'm also slightly thinking ahead to when I run batch scripts upon my application; if not returning to the default console will have any repercussions.

Thanks in advance if anyone can help point me in the right direction.

Chris
  • 2,254
  • 8
  • 22
  • Are you implying that this is a WPF application that outputs messages to a command window (cmd.exe)? – STLDev Mar 07 '18 at 17:32
  • 1
    @Agent_Orange Please refrain from commenting when you clearly don't know the site rules. Also, this is not your typical forum where you write `lel` and `:v` like if you were 14 – Camilo Terevinto Mar 07 '18 at 17:32
  • @Agent_Orange Well to be fair, it's not like I actually _wanted_ to recreate the question. I'm fully aware of the rules, but my question was up for literally 45 seconds and the person who closed it, couldn't have possibly read the issue because the answer it was marked as a duplicate with an outdated WinForms solution that does not work for my particular situation and then closed it. I waited a little bit to see if anyone would at least comment, but I got nothing so I wanted at least a fair attempt at getting some advice. – Chris Mar 07 '18 at 17:33
  • 4
    AttachConsole is a pretty bad idea. There are now *two* programs that read input from the console, the command processor (cmd.exe) and your program. Which is why it looks like you have to press Enter, it gives your app a turn to read input. Cmd.exe displaying the prompt should be noticeable. You must use start /wait yourapp.exe to start your program. A simple hack is to write a very small console mode app that does nothing but start your main program. Rename it to yourapp.com and it is automagically the one that runs from a command prompt. – Hans Passant Mar 07 '18 at 17:34
  • @STLDeveloper Correct! Basically it's an application that can run through the GUI or through the command line. All of the functionality of the application itself works perfectly fine. However, if I run it from the command line, it runs as expected with the exception that it always waits for me to press "Enter" before it actually finishes. – Chris Mar 07 '18 at 17:34
  • 1
    @HansPassant Ohhhh... Okay. See, I'm glad I got a chance to actually get a response like that this time because that was an intricacy I didn't know about. That's something I'll take into consideration and think about a redesign. I appreciate the feedback. Thank you very much for the insight. – Chris Mar 07 '18 at 17:37
  • @Camilo Terevinto hmm i apologize for doing that, but can you please send me a link of rules of commenting cuz i cant find it in help of stackoverflow. – Agent_Orange Mar 08 '18 at 03:18

0 Answers0