Reputation: 10068
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!
Upvotes: 11
Views: 16040
Reputation: 27
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!
Upvotes: 1
Reputation: 138
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.
Upvotes: 1
Reputation: 17402
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;
}
Upvotes: 6
Reputation: 666
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
Upvotes: 1
Reputation: 995
Environment.Exit and Application.Exit
Environment.Exit() is cleaner.
http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx
Upvotes: 6