C Board  

Go Back   C Board > General Programming Boards > C# Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 09-25-2006, 07:08 AM   #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"))
Wintersun is offline  
Old 09-25-2006, 07:11 AM   #2
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
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.
Prelude is offline  
Old 09-25-2006, 07:15 AM   #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.
Wintersun is offline  
Old 09-25-2006, 07:19 AM   #4
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
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.
Prelude is offline  
Old 09-25-2006, 07:24 AM   #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!
Wintersun is offline  
Old 09-25-2006, 11:10 AM   #6
Registered User
 
Join Date: Aug 2005
Posts: 208
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.
dxfoo is offline  
Old 09-25-2006, 01:49 PM   #7
and the Hat of Clumsiness
 
GanglyLamb's Avatar
 
Join Date: Oct 2002
Posts: 1,101
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.
GanglyLamb is offline  
Old 09-25-2006, 05:32 PM   #8
Registered User
 
Join Date: Aug 2005
Posts: 208
I'm just proving you can do it if you really need to, but yeah, Console.ReadLine() is the way to go.
dxfoo is offline  
Old 09-25-2006, 06:09 PM   #9
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Prelude is offline  
Old 09-25-2006, 06:32 PM   #10
Registered User
 
Join Date: Aug 2005
Posts: 208
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.
dxfoo is offline  
Old 09-25-2006, 06:56 PM   #11
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Quote:
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.
Prelude is offline  
Old 09-25-2006, 11:46 PM   #12
Anti-Poster
 
Join Date: Feb 2002
Posts: 1,224
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.
__________________
Rule #1: Every rule has exceptions
pianorain is offline  
Old 10-03-2006, 10:11 AM   #13
Registered User
 
Join Date: Aug 2005
Posts: 208
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.
dxfoo is offline  
Old 10-03-2006, 10:22 AM   #14
System Novice
 
siavoshkc's Avatar
 
Join Date: Jan 2006
Location: Tehran
Posts: 1,075
Quote:
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.
__________________
Microsoft Visual Studio 2008 Professional (On Microsoft Windows XP SP2)

Learn the language before using it. (C++ Books and C Books)
Read the FAQ before making a problem.
Then make a Google and Forum search.

My code painter new version Version 0.97 DOWNLOAD NOW! (Let the pop up, pop!)

SiavoshKC
siavoshkc is offline  
Old 10-03-2006, 10:36 AM   #15
Registered User
 
Join Date: Aug 2005
Posts: 208
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.
dxfoo is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22