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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Wink This is a good answer,isnt it?

    If there is no alternative in C#,we can use System("pause") provided in msvcrt.dll,maybe it is not the best or the easiest way to do that,but we can learn from it,as we will know if we want to use those system calls in a C/C++ program,we can import msvcrt.dll
    I am also a beginner at C#,and I reached here with google when I searched answers to my question,I think dxfoo is helpful to us,isnt he?

    Quote Originally Posted by dxfoo View Post
    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.

  12. #12
    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

  13. #13
    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.

  14. #14
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    You are a free man. I can't remember anyone forcing you to use message boards.

    Some of us find them usefull others like you obviously don't.

    I like my coffee with milk and sugar, others don't.
    Which does not imply I am superior just because I drink it that way.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Who gives a rip if system("") is meant to target Windows and the system of
    >which various system-specific commands reside in.
    Just because you don't care doesn't mean that others don't. It's assumptions like this that produce crappy code.

    >Every professional program goes outside of the CLS to get tasks done such as Win32 and their own specific dll's.
    Really. Well, you're wrong because I can name at least one professional program (that I worked on recently) which uses CLS exclusively.

    >It makes me assume that you're new to development and had no reason to go outside of the sandbox to get tasks done.
    Pot. Kettle. Black.

    >Please, argue about something that's more interesting than what's the best way of calling "pause" on a console window.
    It's not the details, it's the principle. If you suggested an exceptionally poor way of loading employee records from a database via ODBC in a professional payroll system, the discussion would have followed a similar path.

    >I looked and found 90% of them very basic questions and not even worth replying to.
    >None of them are worth my time!
    I can't respond to this in a way that wouldn't break forum rules. You're welcome to leave and never return if this is how you see our community.

    >90% of them are so misinformed or basic
    Look at your own posts before complaining about the posts of others, please.

    >At work we don't talk about what the best way is to pause windows
    If you can imagine, I talk about slightly more important things at work as well. Work related topics. Probably topics well beyond your current experience level. Why? Because the people I work with already know the basics and don't need to debate it.
    My best code is written with the delete key.

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