Thread: system("pause") in C#? (warning: noob question)

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    system("pause") in C#? (warning: noob question)

    Hi guys!

    First of all: I am totally new to the laguage C# - just learning the basics right now. I have some experience with C and C++ though. I just typed a simple program and compiled it, but it's hard to tell whether it's working or not, as the program closes immediately after launch. Is there some kind of pause function that can be used to delay shutdown of the program until the user presses a key? (like the one in C called system("pause"))

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Ask for input. That's usually the best way to go about it. Especially in C# where you have direct control over keypresses and can simulate the getch/kbhit dealie that everyone wants in C++.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    thanks a lot!! :-) now I'm on my way to mastering what appears to be the programming language I've dreamed of..hehe.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    C# is a nice language, but that's mostly due to the .NET framework. I find .NET development to be very pleasant, if mindless a lot of the time because everything is so darn easy to do.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    haha yeah you are right - I guess that's what makes it seem very nice for noobs like me. Haha!

  6. #6
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    You can interop it with msvcrt.dll, I believe. Ah, here's my old reply to the question:

    You have to pinvoke this C function into your C# application. Luckily, this process is very easy. There are a few steps:

    1) Insert System.Runtime.InteropServices to your using clauses.
    2) Insert this line in your class (usually in the first few lines)

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

    3) In that same class, simply write this:
    system("pause");

    Hope that helps! It's not the best solution in C# to go about the problem, but hey, it's a solution.
    Last edited by dxfoo; 09-25-2006 at 11:14 AM.

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    It sure is an ugly solution dxfoo.

    Like prelude said, ask for input.

    Code:
    Console.ReadLine();
    Simple and works fine, no need to work with interop or anything.

    If there is anything you should do with C# is use the classes that are already written. You'll be sure that there are no bugs in it. The only time one can write his own class to replace a class already available in the framework could be for learning purpose.

  8. #8
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I'm just proving you can do it if you really need to, but yeah, Console.ReadLine() is the way to go.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm just proving you can do it if you really need to
    As if that were necessary.
    My best code is written with the delete key.

  10. #10
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Okay, I'm really beginning to wonder if it's me that's always negative or if it is really you guys. The function system("pause"); actually has code that the user isn't able to do with Console.ReadLine() Try writing several letters with system("pause"). It will find one pressed and continue as expected. It will do the opposite with Console.ReadLine(); which may not even be acceptable to the developer. Not to mention, 'press any key to continue' with Console.ReadLine() and you are pressing 500 keys and it will display ALL 500 of them, and the program won't continue until you press enter. It's more like "press enter to continue" and you can type a million things before you do so before pressing 'enter' which is absolutely unprofessional, and I can tell that Prelude falls in this area. They do slightly different things, and it is up to the developer to decide on what he needs to do. I presented a slightly different solution that can give the developer some differnet options so he can choose what is best for his project. If at all, this solution would be more professional towards the end user. This was an absolute waste of time to clear up Prelude's stupid remarks. Options are good. Learn what they are and move on.
    Last edited by dxfoo; 09-25-2006 at 06:59 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm really beginning to wonder if it's me that's always negative or if it is really you guys.
    It's really us. We tend not to take kindly to bad advice, and suggesting overly complicated and inferior solutions is akin to bad advice.
    The function system("pause"); actually has code that the user isn't able to do with Console.ReadLine() Try writing several letters with system("pause"). It just won't happen. It will with Console.ReadLine(); which may not even be acceptable to the developer. Not to mention, 'press any key to continue' and you are pressing 500 keys and it won't do anything. It's more like "press enter to continue" and you can type a million things before you do so which is absolutely unprofessional. They do slightly different things, and it is up to the developer to decide on what he needs to do. I presented a slightly different solution that can give the developer some differnet options so he can choose what is best for his project.
    Your entire argument is nothing more than hot air in light of the solution that I originally suggested. Since it wasn't as obvious to you as it was to me, I'll clarify the complete solution that simulates system("pause") exactly, with code:
    Code:
    public static void Pause()
    {
      Console.Write("Press any key to continue . . . ");
      Console.ReadKey(true);
    }
    QED
    My best code is written with the delete key.

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Despite the absence of need, I just thought I'd chime in.
    Quote Originally Posted by dxfoo
    If at all, this solution would be more professional towards the end user.
    Define "end user" and guarantee to me that you wouldn't have to port your implicit PInvoke. This solution breaks any hope of being CLS compliant. Please help me understand why you'd even suggest such a poor solution when there are obvious superior ones.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Interesting idea on the Console.ReadKey() method, but all these insults are very pointless, though. And CLS compliant? Who gives a rip if system("") is meant to target Windows and the system of which various system-specific commands reside in. Pausing a "console" isn't needed outside of the CLS, but I stated that system() can still be called if wanted. Every professional program goes outside of the CLS to get tasks done such as Win32 and their own specific dll's. It makes me assume that you're new to development and had no reason to go outside of the sandbox to get tasks done. Please, argue about something that's more interesting than what's the best way of calling "pause" on a console window. This is just scaring me.
    Last edited by dxfoo; 10-03-2006 at 10:21 AM.

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Please, argue about something that's more interesting than what's the best way of calling "pause" on a console window. I can't believe you guys have nothing better to do.
    Yeah, go to my threads and answer at least one of them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I looked and found 90% of them very basic questions and not even worth replying to. You might want to try harder and then the sarcasm. But quite honestly, I'm sick of message boards finally. I'm working with great professionals here at work, and I come home every other day to check up on the latest posts at various message boards. None of them are worth my time! 90% of them are so misinformed or basic that I wonder what the hell just happened from work and to these boards. I just figure these boards are common to the average person who has nothing better to do, so perhaps it's just time to leave as most folks I know who did the same thing. At work we don't talk about what the best way is to pause windows I know, it's shocking! And I don't miss it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. Quick Warning Question
    By Paul22000 in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 05:20 PM
  4. Real Noob question
    By Dark Greek in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2007, 06:49 PM
  5. super noob question
    By verbity in forum C++ Programming
    Replies: 6
    Last Post: 06-23-2007, 11:38 AM