none
How to write a command like "pause" to console

    Question

  • People I'm new to C# and I want to ask (if someone could help)

    How to convert a code like this (in C++) to C#:

    #include<iostream>
    #include "stdafx.h"
    #include "test.h"
    using namespace std;

    int main()
    {
     cout<<"Helllo";
     system("pause");          //This one :)
     return 0;
    }

    Wednesday, May 24, 2006 6:04 AM

Answers

  • Hi

    In C# the Code can be written like this using Console Applications :-


    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace Pause {

                class Program {

                            static void Main(string[] args)

                            {

                                        Console.WriteLine("Hello");

                                        Console.ReadLine(); //Pause

                            }

                }

    }


    Happy Coding

    Wednesday, May 24, 2006 6:51 AM

All replies

  • Console.ReadLine()

    Will this work or am I missing something?

     

    Wednesday, May 24, 2006 6:13 AM
  • Hi,

    create new console project in VS.NET, add the following code to the main method:
    System.Diagnostics.Process.Start("pause");

    Wednesday, May 24, 2006 6:15 AM
  • Hi

    In C# the Code can be written like this using Console Applications :-


    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace Pause {

                class Program {

                            static void Main(string[] args)

                            {

                                        Console.WriteLine("Hello");

                                        Console.ReadLine(); //Pause

                            }

                }

    }


    Happy Coding

    Wednesday, May 24, 2006 6:51 AM
  •  Bulldog.NET wrote:

    Hi

    In C# the Code can be written like this using Console Applications :-


    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace Pause {

                class Program {

                            static void Main(string[] args)

                            {

                                        Console.WriteLine("Hello");

                                        Console.ReadLine(); //Pause

                            }

                }

    }


    Happy Coding



    use

    Console.WriteLine("Press any key to continue...");
    Console.ReadKey(true);

    in place of

    Console.ReadLine();
    • Proposed as answer by Tarec Wednesday, April 10, 2013 4:34 PM
    Wednesday, February 27, 2008 8:26 PM
  • The above solution works:

    Console.WriteLine("Press any key to continue...");
    Console.ReadKey(true);

    Although I found if you use it when debugging it produces duplicate output.
    The solution when debugging is:

    1. Comment out the Console.Writeline and Console.ReadKey lines.
    2. Use Console.Write("\n");
        - This will produce a 'newline' and prompt "Press any key to continue..."
    3. Once debugging is completed, either remove or comment the Console.Write() and uncomment the previous two lines.

    I'm not sure why it works this way in debugging mode; therefore, any elaboration would be greatly appreciated.

    Hope that helps.

    • Edited by A. Zuniga Monday, September 29, 2008 6:29 AM
    Monday, September 29, 2008 6:18 AM
  • Hi,

    the easiest way to replace the

    system("pause");

    command in c# is to use something like this method I created exactly for that purpose:

    /// <summary>
    /// Writes a message to the console prompting the user to press a certain key in order to exit the current program.
    /// This procedure intercepts all keys that are pressed, so that nothing is displayed in the Console. By default the
    /// Enter key is used as the key that has to be pressed.
    /// </summary>
    /// <param name="key">The key that the Console will wait for. If this parameter is omitted the Enter key is used.</param>
    public static void WriteKeyPressForExit(ConsoleKey key = ConsoleKey.Enter)
    {
    	Console.WriteLine();
    	Console.WriteLine("Press the {0} key on your keyboard to exit . . .", key);
    	while (Console.ReadKey(intercept: true).Key != key) { }
    }

    However... when invoking the pause command you always get a message in the language of your current operating system... So if you'd like to display the default OS pause method you could also use this:

    By the way... for this method you need to reference the System assembly (which is always referenced in new ConsoleApplication-projects in VS)

    /// <summary>
    /// Emulates the pause command in the console by invoking a cmd-process. This method blocks the execution until the user has pressed a button.
    /// </summary>
    public static void Pause()
    {
    	Console.WriteLine();
    	System.Diagnostics.Process pauseProc =
    		System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = "cmd", Arguments = "/C pause", UseShellExecute = false });
    	pauseProc.WaitForExit();
    }

    This method just invokes a pause command inside a new cmd process... Note the "UseShellExecute = false" statement which causes the process to start inside you current console application instead of openening a new window...

    The ultimate go-around would be to invoke the system function directly like in C++... To do that however we need to import the dll the system function resides in which in itself is not that nice... (ok, neither is invoking a cmd process... ^^)

    To do that you'd need to do something like this:

    [System.Runtime.InteropServices.DllImport("msvcrt.dll")]
    public static extern bool system(string str);

    By the way... Does anybody know why I get a PInvokeStackImbalance warning when debugging over that method???
    Sunday, July 08, 2012 5:59 PM
  • In my case it works
    Monday, August 27, 2012 2:57 AM
  • To do that you'd need to do something like this:

    [System.Runtime.InteropServices.DllImport("msvcrt.dll")]
    public static extern bool system(string str);

    By the way... Does anybody know why I get a PInvokeStackImbalance warning when debugging over that method???

    Because it's a C function which should be called using the Cdecl call convention:

    [System.Runtime.InteropServices.DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
    Monday, August 27, 2012 2:32 PM
  • Thank you my friend. I'm new in developer. Thanks for the script "Console.ReadLine();"

    Thursday, May 09, 2013 8:51 PM
  • The problem with Console.ReadKey(); or Console.ReadLine(); is that it locks your Console window from responding to any additional `Console.Write` commands. If you want to pause the window, but still be able to write out information to the Console you need to start your work on a new Thread and then call Join on the current thread as shown below.
            
        static void Main(string[] args)
        {
            MyWork worker = new MyWork();

            ThreadStart job = new ThreadStart(worker.StartMyWork);
            Thread thread = new Thread(job);

            thread.Start();

            Thread.CurrentThread.Join();
        }

    • Edited by dd3000 Friday, June 21, 2013 2:25 PM Edit for format
    Friday, June 21, 2013 2:25 PM