Why is the console window closing immediately once displayed my output?

前端 未结 21 2276
死守一世寂寞
死守一世寂寞 2020-11-22 03:45

I\'m studying C# by following the guides in MSDN.

Now, I just tried the Example 1 (here is the link to MSDN), and I\'ve encountered an i

相关标签:
21条回答
  • 2020-11-22 03:48

    Instead of using

    Console.Readline()
    Console.Read()
    Console.ReadKey()
    

    you can run your program using Ctrl+F5 (if you are in Visual Studio). Then Visual Studio will keep the console window open, until you press a key.

    Note: You cannot debug your code in this approach.

    0 讨论(0)
  • 2020-11-22 03:49

    The program immediately closes because there's nothing stopping it from closing. Insert a breakpoint at return 0; or add Console.Read(); before return 0; to prevent the program from closing.

    0 讨论(0)
  • 2020-11-22 03:49

    Another way is to use Debugger.Break() before returning from Main method

    0 讨论(0)
  • 2020-11-22 03:50

    The code is finished, to continue you need to add this:

    Console.ReadLine();
    

    or

    Console.Read();
    
    0 讨论(0)
  • 2020-11-22 03:51

    this is the answer async at console app in C#?

    anything whereever in the console app never use await but instead use theAsyncMethod().GetAwaiter().GetResult();,

    example

    var result = await HttpClientInstance.SendAsync(message);

    becomes

    var result = HttpClientInstance.SendAsync(message).GetAwaiter().GetResult();

    0 讨论(0)
  • 2020-11-22 03:52

    If you want to keep your application opened, you have to do something in order to keep its process alive. The below example is the simplest one, to be put at the end of your program:

    while (true) ;
    

    However, it'll cause the CPU to overload, as it's therefore forced to iterate infinitely.

    At this point, you can opt to use System.Windows.Forms.Application class (but it requires you to add System.Windows.Forms reference):

    Application.Run();
    

    This doesn't leak CPU and works successfully.

    In order to avoid to add System.Windows.Forms reference, you can use a simple trick, the so-called spin waiting, importing System.Threading:

    SpinWait.SpinUntil(() => false);
    

    This also works perfectly, and it basically consists of a while loop with a negated condition that is returned by the above lambda method. Why isn't this overloading CPU? You can look at the source code here; anyway, it basically waits some CPU cycle before iterating over.

    You can also create a message looper, which peeks the pending messages from the system and processes each of them before passing to the next iteration, as follows:

    [DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "PeekMessage")]
    public static extern int PeekMessage(out NativeMessage lpMsg, IntPtr hWnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg);
    
    [DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "GetMessage")]
    public static extern int GetMessage(out NativeMessage lpMsg, IntPtr hWnd, int wMsgFilterMin, int wMsgFilterMax);
    
    [DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "TranslateMessage")]
    public static extern int TranslateMessage(ref NativeMessage lpMsg);
    
    [DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "DispatchMessage")]
    public static extern int DispatchMessage(ref NativeMessage lpMsg);
    
    [DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode]
    public static bool ProcessMessageOnce()
    {
        NativeMessage message = new NativeMessage();
    
        if (!IsMessagePending(out message))
            return true;
    
        if (GetMessage(out message, IntPtr.Zero, 0, 0) == -1)
            return true;
    
        Message frameworkMessage = new Message()
        {
            HWnd = message.handle,
            LParam = message.lParam,
            WParam = message.wParam,
            Msg = (int)message.msg
        };
    
        if (Application.FilterMessage(ref frameworkMessage))
            return true;
    
        TranslateMessage(ref message);
        DispatchMessage(ref message);
    
        return false;
    }
    

    Then, you can loop safely by doing something like this:

    while (true)
        ProcessMessageOnce();
    
    0 讨论(0)
提交回复
热议问题