Zombie processes in VS 2013 console applications - c#

I'm trying to create simple console apps in VS 2013, C#. Win 7. Running in debug, the window closes immediately (no "Press any key..."). Running without debug, the window simply appears and hangs.
The only way out is to close the window, but then I get zombie processes which can only be shut down by rebooting.
I know this was a problem under 2008, but I thought it had been fixed. Ideas?

Console applications won't automatically wait around after execution completes. You have to add in a call to ReadKey() for that to happen.
static void Main(string[] args)
{
/* all your code goes here */
Console.WriteLine("Press any key ...");
Console.ReadKey();
}
Could you please elaborate on what the zombie processes are (via Task Manager) and maybe we can help identify them?

Related

problem in code the apps just exits although the code is fine [duplicate]

This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 5 years ago.
My console applications on Visual Studio are closing automatically once the program finishes the execution. I'd like to "pause" the applications at the end of their execution so that I can easily check the output. How can I achieve that?
Update:
As of Visual Studio 2017, there's a built-in option in the IDE at:
Tools > Options > Debugging > Automatically close the console when debugging stops
Unchecking it will automatically pause the application at the end of the execution, allowing you the easily check its output without having to add extra code to your application.
There are two ways;
Console.ReadLine();
ReadLine() waits for ↩
or
Console.ReadKey();
ReadKey() waits for any key (except for modifier keys).
You can just compile (start debugging) your work with Ctrl+F5.
Try it. I always do it and the console shows me my results open on it. No additional code is needed.
Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.Readline() or ReadKey() functions.
Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.
Use:
Console.ReadKey();
For it to close when someone presses any key, or:
Console.ReadLine();
For when the user types something and presses enter.
Alternatively, you can delay the closing using the following code:
System.Threading.Thread.Sleep(1000);
Note the Sleep is using milliseconds.
Ctrl + F5 is better, because you don't need additional lines.
And you can, in the end, hit enter and exit running mode.
But, when you start a program with F5 and put a break-point, you can debug your application and that gives you other advantages.
Those solutions mentioned change how your program work.
You can off course put #if DEBUG and #endif around the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...' when running from the command line, the way to go is to use the System.Diagnostics.Debugger API's.
If you only want that to work in DEBUG, simply wrap this code in a [Conditional("DEBUG")] void BreakConditional() method.
// Test some configuration option or another
bool launch;
var env = Environment.GetEnvironmentVariable("LAUNCH_DEBUGGER_IF_NOT_ATTACHED");
if (!bool.TryParse(env, out launch))
launch = false;
// Break either if a debugger is already attached, or if configured to launch
if (launch || Debugger.IsAttached) {
if (Debugger.IsAttached || Debugger.Launch())
Debugger.Break();
}
This also works to debug programs that need elevated privileges, or that need to be able to elevate themselves.
If you do not want the program to close even if a user presses anykey;
while (true) {
System.Console.ReadKey();
};//This wont stop app

Output screen disappears suddenly in C# console application?

I am writing a simple C# console application in VS 2013 but the problem is that output screen flashes for a moment and disappears suddenly . I use the alternate way and write Console.Readline() method in the end and problem is fixed .So but i personally think that this is just a trick to stop the screen and not the proper way.So can anyone explain me the proper way of doing this ??
The program immediately closes because there's nothing stopping it from closing. Insert a breakpoint at return 0; or add Console.Read(); before return 0; to prevent the program from closing.
After you are done with your program, press Ctrl+F5 ( Run without
debugging). This will prompt before closing the window and this is
what you want.
Or use this line at the end
Console.ReadKey();
No, this is the correct way of doing it. Console.ReadLine() and Console.ReadKey() are a blocking statement: they halt the thread to wait for input before continueing. If you wouldn't do this the program would reach its end and thus exit the console.
In a larger console program you might have a while(running) loop instead and a GUI won't have this problem until its GUI thread is explicitly stopped (closing the window), but for a simple console app you can just use this 'trick'.
Yes, as long as you don't stop the application it keeps running and ends when done processing.
That's why you need the Console.ReadLine(), it stops processing and waits until you tell it to continue.
when you run the program in Debug mode if you do'nt have any BreakPoints or if .NET engine does not need to Read IO from user then it just finishes the excution without waiting anymore.
if you want to stop the execution at the end want to see the console screen you can either use any IO Read Statements as below:
Console.ReadLine();
Console.Read();
Console.ReadKey();
OR
You can run your program in without Debugging mode by simply pressing Ctrl+F5

c# console wont close after program ends

I have c# application that I am running, and then in some point application throws an error which is then catched, then app should end. And it ends, but console windows stays open...
I even checked in windows task manager, under applications tab, there is listed my console, but when I click go to process, there is no process of that application.
Thats weird... Application ended, process ended, but console stays on? How can I kill that console?
Edit: my code:
static class Program
{
static void Main()
{
try
{
//bunch of static methods from other static classes are being invoked
Setup.Driver.Close();//another static method
}
catch (Exception)
{
Setup.Driver.Close();
}
}
}
Second edit: Note: Process.Getprocess().Kill(), Application.Exit(), Environment.Exit() are not working for me, in windows task manager, there is no process left to kill, only console stays open!
Environment.Exit(0);
or
this.Close();
If you have threads running, you can try this brute force method before you call Exit:
using System.Diagnostics;
ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;
foreach (var thread in currentThreads)
{
thread.Interupt(); // If thread is waiting, stop waiting
// or
thread.Abort(); // Terminate thread immediately
// or
thread.IsBackGround = true;
}
Environment.Exit and Application.Exit
Environment.Exit() is cleaner.
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
Good morning,
is the console app not closing when you start it from Visual Studio with the debugger attached or is it also not closed when you launch it from the file system / without attached debugger?
When the debugger is attached, you will always see an 'Press ENTER to exit ...' (or similar message).
When talking about the task manager - do you see the *.vshost process in there?
If yes, this is 'required' by Visual Studio and is not your 'real' console application; you will always see a *.vshost process when launching executables from within Visual Studio.
Hope this helps
Your program is most likely linked to another process which keeps it open.
In my case, the process was chromedriver.
Check your processes and your code to see what you opened and what's still running in the background.
I was stuck with this issue too. Once I was done with the browser, I just used Driver.Close(); However, doing this still kept chromedriver running in the background. So instead I used Driver.Quit(); . Then I made sure to return; from the Main method.
Hope this helps!
Environment.Exit();
will this work?

How to stop C# console applications from closing automatically? [duplicate]

This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 5 years ago.
My console applications on Visual Studio are closing automatically once the program finishes the execution. I'd like to "pause" the applications at the end of their execution so that I can easily check the output. How can I achieve that?
Update:
As of Visual Studio 2017, there's a built-in option in the IDE at:
Tools > Options > Debugging > Automatically close the console when debugging stops
Unchecking it will automatically pause the application at the end of the execution, allowing you the easily check its output without having to add extra code to your application.
There are two ways;
Console.ReadLine();
ReadLine() waits for ↩
or
Console.ReadKey();
ReadKey() waits for any key (except for modifier keys).
You can just compile (start debugging) your work with Ctrl+F5.
Try it. I always do it and the console shows me my results open on it. No additional code is needed.
Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.Readline() or ReadKey() functions.
Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.
Use:
Console.ReadKey();
For it to close when someone presses any key, or:
Console.ReadLine();
For when the user types something and presses enter.
Alternatively, you can delay the closing using the following code:
System.Threading.Thread.Sleep(1000);
Note the Sleep is using milliseconds.
Ctrl + F5 is better, because you don't need additional lines.
And you can, in the end, hit enter and exit running mode.
But, when you start a program with F5 and put a break-point, you can debug your application and that gives you other advantages.
Those solutions mentioned change how your program work.
You can off course put #if DEBUG and #endif around the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...' when running from the command line, the way to go is to use the System.Diagnostics.Debugger API's.
If you only want that to work in DEBUG, simply wrap this code in a [Conditional("DEBUG")] void BreakConditional() method.
// Test some configuration option or another
bool launch;
var env = Environment.GetEnvironmentVariable("LAUNCH_DEBUGGER_IF_NOT_ATTACHED");
if (!bool.TryParse(env, out launch))
launch = false;
// Break either if a debugger is already attached, or if configured to launch
if (launch || Debugger.IsAttached) {
if (Debugger.IsAttached || Debugger.Launch())
Debugger.Break();
}
This also works to debug programs that need elevated privileges, or that need to be able to elevate themselves.
If you do not want the program to close even if a user presses anykey;
while (true) {
System.Console.ReadKey();
};//This wont stop app

C# App using Windows API Code Pack Shell Extension won't shut down

I have a C# application which makes use of the Microsoft Windows API Code Pack - in particular the Shell Extensions, which I use to monitor storage devices and media insertion and removal.
However when I attempt to close the app in Visual C# 2010 (Express) I then have to manually stop the debugger. It appears that there is a background loop in the Win API Code Pack that is still running, even when I manually dispose of the ShellObjectWatcher. The only way I can kill it is to manually stop the debugger.
The app is built in WPF.
Eventually, VisC#2010 gives up on trying to run the app under the debugger. You tell it to start debugging and it just doesn't. Only way to get it going again is to kill the app using Task Manager and then shutdown VC#2010 - go have a coffee - then start it up again. Odd. I suspect there is a hidden process or window hanging around which isn't being shut down when I try and clean up the app.
Any idea how I can clean up this ShellObjectWatcher a little more effectively?
To fix the bug in the Code Pack Shell project, add one line in MessageListener.WndProc()​:
case (uint)WindowMessage.Destr​oy:
**_running = false;**
break;
Now ThreadMethod() will exit the message loop.
OK using System.Environment.Exit(0); fixes this issue. App shuts down and the debugger releases control. Brute force works in this case.
I bumped into the same issue, but solved it by setting the thread as a background thread (meaning, it is killed when the application stops):
in MessageListener.cs, in the constructor, at line 40, I have:
_windowThread = new Thread(ThreadMethod);
// The line below will force the thread to terminate when the app exits.
_windowThread.IsBackground = true;
_windowThread.SetApartmentState(ApartmentState.STA);

Categories