The program '[11476] ConsoleApp22.exe' has exited with code 0 (0x0) - c#

using System;
// Namespace
namespace ConsoleApp22
{
// Main Class
class Program
{
// Entry Point Method
static void Main(string[] args)
{
string name = "Florent Shomora";
// start here
Console.Write("Hello World"+ name);
}
}
}
This is my code and the problem is that the console pop up and die.

Do you want to see console window ? Try to use Console.Readline()
static void Main(string[] args)
{
string name = "Florent Shomora";
// start here
Console.Write("Hello World"+ name);
Console.Readline();
}

The problem is that your program works without error.
However, what does your program do? It prints a message to the screen, nothing more. This is a very fast operation. The program will complete this operation and terminate probably faster than you can read the message.
You can pause the program by adding another operation which will take more time, such as expecting user input:
string name = "Florent Shomora";
// start here
Console.Write("Hello World"+ name);
Console.ReadLine();
With that extra call to ReadLine() the application will now remain open and wait for you to press "return" before closing.

Try
Console.Readline()
it should show u the Console and u can exit by clicking for example enter

Related

Console app only terminates when holding key down while running batch script

I am trying to create a C# console application that repeatedly runs a .bat file and saves the output into a variable to be modified later. The script is meant to get open TCP connections on a connected device using adb.exe.
I want the application to quit when the Esc key is pressed (once). To accomplish this I followed this answer and implemented it like so:
Program.cs
static void Main(string[] args)
{
Console.WriteLine("Press escape to quit");
do
{
while (!Console.KeyAvailable)
{
// Console.WriteLine("Application is running");
RunBatch();
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
Console.WriteLine("While loop was exited");
Console.ReadLine();
}
static void RunBatch()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"C:\Dev\Batch\GetTcpConnections.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
}
GetTcpConnections.bat
#echo off
echo %time%
adb.exe shell cat /proc/net/tcp
timeout /t 1 /nobreak>nul
The expected result would be, that the console application exits the loop and prints While loop was exited directly upon pressing Esc. It works like this when I comment RunBatch() and un-comment Console.WriteLine("Application is running". However, when I am running the batch script, I need to hold down Esc for about a second or two before the program exits the while loop, instead of being instantaneous.
At first I thought that the input may be blocked by timeout /t 1 /nobreak>nul in the batch script, but removing this line made no difference. Am I missing something else here that could block inputs?
As soon as your consoleapp starts adb.exe, it looses the focus. When an applicaties does not have the focus, it does not receive any keyboard input, because the keyboard input goes to another focused application.
You can reclaim the focus by selecting the consoleapp with your mouse, while adb.exe is running, and than press ESC. But I guess that is not what you want.
I see serveral "solutions":
You could find a way to make your consoleapp always the top-level applicatie.
Make it a Desktop / Winform-application which has a bug "QUIT"-button.
The code below should solve your problem. Note that I have moved the timeout away from the batch file and placed it within the while loop.
Program.cs
private static void Main(string[] args)
{
Console.WriteLine("Press escape to quit");
do
{
while (!Console.KeyAvailable)
{
RunBatch();
Thread.Sleep(1000);
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
Console.WriteLine("While loop has exited");
Console.ReadLine();
}
private static void RunBatch()
{
var process = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = #"C:\Dev\Batch\GetTcpConnections.bat"
}
};
process.Start();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
GetTcpConnections.bat
#echo off
echo %time%
adb.exe shell cat /proc/net/tcp

How do I stop my C# console program from stopping when executing "console.readline();" twice

i'm really new to C# and i've been working on this really simple command line style program (that has custom commands and such). Now the commands work great but every time I allow the user to go back to enter another command or just anything it closes the program when I press enter. But only the second time I execute a command. I think this has something to do with console.WriteLine();
Here's my code (I've searched everywhere on how to fix this and nothing that i've found has worked)
using System;
namespace ConsoleProgram
{
class Program
{
private static string userEnteredCommand;
static void Main(string[] args)
{
Console.Title = "IAO Systems Service Console";
onCommandLineStart();
void onCommandLineStart()
{
Console.WriteLine("Copyright (C) 2018 IAO Corporation");
Console.WriteLine("IAO Systems Service Console (type 'sinfo' for more information.");
userEnteredCommand = Console.ReadLine();
}
void onCommandLineReturn()
{
userEnteredCommand = Console.ReadLine();
}
// Commands
if (userEnteredCommand == "sinfo")
{
Console.WriteLine(" ");
Console.WriteLine("Program information:");
Console.WriteLine("Created for IAO Corporation, by Zreddx");
Console.WriteLine("This program controls doors, gates and e.t.c within IAO Terratory.");
Console.WriteLine(" ");
Console.WriteLine("This program is protected by copyright, do not redistribute. ");
}
else
{
Console.WriteLine("That command does not exist, do 'programs' for a list of actions.");
}
onCommandLineReturn();
}
}
}
Console applications close when they get to the end of Main. It's exiting after the Console.ReadLine in onCommandLineReturn();.
Add a bool variable called keepLooping, set it to true, and wrap your code in a while(keepLooping) statement. Somewhere in your program flow, check for input like "quit" or "exit" and set the keepLooping variable to false.
Here's an example of it in a dotnetfiddle: https://dotnetfiddle.net/Jguj5k

Console.ReadLine doesn't wait for input

I was writing this code and noticed that the Console.ReadLine() command doesn't wait for input. Those lines are being executed for sure but for some reason the console just closes on me without being able to type.
The class is set to be console application.
public void Register()
{
Console.WriteLine("Enter User Name to Register:");
String nickname = Console.ReadLine();
User newUser = new User(nickname); //todo: check if it doesn't already exist//
this.userList.Add(newUser);
persistantLayer.Saver.saveUser(newUser);
}

Multiple IFs won't work

I'm creating a Desktop application (console) for Windows, using C#.
This is my code:
namespace myapp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello!");
Console.WriteLine("Type 'exit' to exit!");
string line = Console.ReadLine();
if (line == "exit") {
Environment.Exit(0);
}
if (line == "copyright") {
Console.WriteLine("Copyright 2017 TIVJ-dev");
}
}
}
}
If I type "exit", it works fine (I'm sure it does this Environment.Exit(0); action). But if I type "copyright", it does not work. I can see an empty line instead. I started with C# today, so my apologies if this is very beginner problem. I haven't found solution on the internet.
Screenshot:
I'm not sure in what way it doesn't work. It should run
Console.WriteLine("Copyright 2017 TIVJ-dev");
then immediately close the console window. Try putting another
Console.ReadLine();
at the end of Main().
You could use an else if statement;
namespace myapp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello!");
Console.WriteLine("Type 'exit' to exit!");
string line = Console.ReadLine();
if (line == "exit")
{
Environment.Exit(0);
}
else if (line == "copyright") {
Console.WriteLine("Copyright 2017 TIVJ-dev");
}
}
}
}
Edit
By using an if/else this does the same as using multiple if statements however this is a more efficient way and make it easier when using break points.
Try launching the application by pressing ctrl F5 in visual studio and seeing if it works. Alternatively click on debug->start without debugging.
Then write "copyright" and press enter.
What is probably happening is that the console is printing the line, then closing before you have a chance to see it. When you don't use the debugger the console stays open even once the application is finished.
Now everyone: Now this project works fine! Thanks for people who said add another Console.ReadLine. When I tested else if, I forgot there was that also. But now it works, so I try to close this. Thanks everyone!

How do I use command line arguments in my C# console app?

I am writing a url shortener app and I would like to also create a console app with C# to push the URLs to a WCF service which I have also created.
WCF app will shorten the url on this URI;
http://example.com/shorten/http://exaple.com
so what I want is just that.
My console exe file will be sitting inside c:\dev folder and on Windows command line, I would like to do this;
c:\dev>myapp -throw http://example.com
with this method I would like to talk to that service. there is no problem on talking part. But the problem is how can I supply this -throw thing on the command line and get a response and put that response on the command line and supply a method to copy that to the clipboard. Am I asking too much here? :S I don't know.
Could you direct me somewhere that I can find information on that or could u please give me an example code of this?
Thanks.
EDIT :
I have tried the following code;
class Program {
static void Main(string[] args) {
if (args[0] == "-throw") {
System.Windows.Forms.Clipboard.SetDataObject(args[1]);
Console.WriteLine(args[1] + " has been added to clipboard !");
Console.ReadLine();
}
}
}
and I received the following error;
C:\Apps\ArgsTry\ArgsTry\bin\Debug>ArgsTry
-throw man
Unhandled Exception:
System.Threading.ThreadStateException:
Current thread must be set to single
thread apartment (STA) mode before OLE
calls can be made. Ensur e that your
Main function has STAThreadAttribute
marked on it. at
System.Windows.Forms.Clipboard.SetDataObject(Object
data, Boolean copy, In t32 retryTimes,
Int32 retryDelay) at
System.Windows.Forms.Clipboard.SetDataObject(Object
data) at
ArgsTry.Program.Main(String[] args) in
c:\apps\ArgsTry\ArgsTry\Program.cs:
line 14
C:\Apps\ArgsTry\ArgsTry\bin\Debug>
Passing arguments to a console application is easy:
using System;
public class CommandLine
{
public static void Main(string[] args)
{
for(int i = 0; i < args.Length; i++)
{
if( args[i] == "-throw" )
{
// call http client args[i+1] for URL
}
}
}
}
As for the clipboard, see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
See the args below, you can use it to read all the values passed when you run your exe file.
static void Main(string[] args) {

Categories