I am using System.Diagnostics in c#. My problem is I am using code for starting and killing the process in c# and it's working fine. But my question is my process id is changing in-between while running the process. Is that possible. If yes then how can I get the actual process id which I want to kill. I can't do it by name because I have multiple instance are running on different at a time and I want to kill only single instance on started port.
My code is :
Process p2 = new Process();
ProcessStartInfo processStartInfo2 =
new ProcessStartInfo(
unitoolLauncherExePath,
"options --port " + port);
p2.StartInfo = processStartInfo2;
p2.Start();
System.Threading.Thread.Sleep(1000);
int processId = p2.Id;
Now it will return something like 14823 and when I am trying to kill it it's changed.
Process[] _proceses = null;
_proceses = Process.GetProcessesByName("UNIToolAPIServer");
foreach (Process proces in _proceses)
{
if (proces.Id == processId)
{
proces.Kill();
}
}
But here nothing is killed because no process with the above id is fetched.
No, the process id of a running process does not change while it is running.
If there is no process to kill with the process id of the process you started, it means either of two things:
The process has already exited before you obtain the process list.
The name of the process is not "UNIToolAPIServer".
If you want to kill the created process you should keep the process-object and call the kill method on it. There should be no need to go thru all the processes in the system to find the started process. For example:
public class MyProcess{
private Process myProcess;
...
public void Start(){
myProcess = new Process();
var processStartInfo2 = new ProcessStartInfo(
unitoolLauncherExePath,
"options --port " + port);
myProcess.StartInfo = processStartInfo2;
myProcess.Start();
}
public void Kill(){
if(myProcess != null && !myProcess.HasExited){
myProcess.Kill();
}
}
}
I made a program that opens COM6. The program starts when the user logs on.
If another user logs on, while the first user is still logged in, the program crazes because the COM is already open.
I found this code, which I thought could solve the problem. The code was meant to close all other application with the same name, but apparently, it does not work, when the other app is running under another user. Have anybody got any solution for this ?
void CloseAllButMe()
{
Process[] processes;
Process self = Process.GetCurrentProcess();
processes = Process.GetProcessesByName(self.ProcessName);
foreach (Process p in processes)
{
if (self.Id != p.Id) p.CloseMainWindow();
}
}
You can use the methods Process.Kill to stop a process. Calling Kill will immediately stop the process and could cause a loss of work.
Here is a code sample for killing Calculator:
public static void KillPaint()
{
System.Diagnostics.Process[] procs = null;
try
{
procs = Process.GetProcessesByName("calc");
Process mspaintProc = procs[0];
if (!mspaintProc.HasExited)
{
mspaintProc.Kill();
}
}
finally
{
if (procs != null)
{
foreach (Process p in procs)
{
p.Dispose();
}
}
}
}
I want to troll my brother a Little bit ;) by writing a program, which I can put in his "StartUp"-Folder. This program should scan his default system tasks so that it doesn't destroy his computer so I wrote all currently open processes in a list. Now I want to check when he opens a program (Process) is it in the list("Taskmgr" is also in the list, so you can exit the troll anytime)? If the opened Process is not in the list, kill it. If you Need any further information, please ask...
My current code is this:
void CloseProcesses()
{
Process[] arrProcesses = Process.GetProcesses();
List<string> lststrProcessNames = new List<string>();
/*Writes current running processes(+ taskmanager process) in a list*/
foreach (Process CurrentProcess in arrProcesses)
{
lststrProcessNames.Add(CurrentProcess.ProcessName);
}
lststrProcessNames.Add("taskmgr");
try
{
Process[] arrNewProcesses = Process.GetProcesses();
foreach (Process NewCurrentProcess in arrNewProcesses)
{
if (lststrProcessNames.Contains(NewCurrentProcess.ProcessName))
{
CloseProcesses();
}
else
{
NewCurrentProcess.Kill();
}
}
}
catch
{
this.Close();
}
}
I want to build my application with the function to restart itself. I found on codeproject
ProcessStartInfo Info=new ProcessStartInfo();
Info.Arguments="/C choice /C Y /N /D Y /T 3 & Del "+
Application.ExecutablePath;
Info.WindowStyle=ProcessWindowStyle.Hidden;
Info.CreateNoWindow=true;
Info.FileName="cmd.exe";
Process.Start(Info);
Application.Exit();
This does not work at all...
And the other problem is, how to start it again like this?
Maybe there are also arguments to start applications.
Edit:
http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=31454&av=58703
I use similar code to the code you tried when restarting apps. I send a timed cmd command to restart the app for me like this:
ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Application.Exit();
The command is sent to the OS, the ping pauses the script for 2-3 seconds, by which time the application has exited from Application.Exit(), then the next command after the ping starts it again.
Note: The \" puts quotes around the path, incase it has spaces, which cmd can't process without quotes.
Hope this helps!
Why not use
Application.Restart();
??
More on Restart
Why not just the following?
Process.Start(Application.ExecutablePath);
Application.Exit();
If you want to be sure the app does not run twice either use Environment.Exit(-1) which kills the process instantaneously (not really the nice way) or something like starting a second app, which checks for the process of the main app and starts it again as soon as the process is gone.
You have the initial application A, you want to restart.
So, When you want to kill A, a little application B is started, B kill A, then B start A, and kill B.
To start a process:
Process.Start("A.exe");
To kill a process, is something like this
Process[] procs = Process.GetProcessesByName("B");
foreach (Process proc in procs)
proc.Kill();
A lot of people are suggesting to use Application.Restart. In reality, this function rarely performs as expected. I have never had it shut down the application I am calling it from. I have always had to close the application through other methods such as closing the main form.
You have two ways of handling this. You either have an external program that closes the calling process and starts a new one,
or,
you have the start of your new software kill other instances of same application if an argument is passed as restart.
private void Application_Startup(object sender, StartupEventArgs e)
{
try
{
if (e.Args.Length > 0)
{
foreach (string arg in e.Args)
{
if (arg == "-restart")
{
// WaitForConnection.exe
foreach (Process p in Process.GetProcesses())
{
// In case we get Access Denied
try
{
if (p.MainModule.FileName.ToLower().EndsWith("yourapp.exe"))
{
p.Kill();
p.WaitForExit();
break;
}
}
catch
{ }
}
}
}
}
}
catch
{
}
}
Winforms has the Application.Restart() method, which does just that. If you're using WPF, you can simply add a reference to System.Windows.Forms and call it.
Another way of doing this which feels a little cleaner than these solutions is to run a batch file which includes a specific delay to wait for the current application to terminate. This has the added benefit of preventing the two application instances from being open at the same time.
Example windows batch file ("restart.bat"):
sleep 5
start "" "C:\Dev\MyApplication.exe"
In the application, add this code:
// Launch the restart batch file
Process.Start(#"C:\Dev\restart.bat");
// Close the current application (for WPF case)
Application.Current.MainWindow.Close();
// Close the current application (for WinForms case)
Application.Exit();
My solution:
private static bool _exiting;
private static readonly object SynchObj = new object();
public static void ApplicationRestart(params string[] commandLine)
{
lock (SynchObj)
{
if (Assembly.GetEntryAssembly() == null)
{
throw new NotSupportedException("RestartNotSupported");
}
if (_exiting)
{
return;
}
_exiting = true;
if (Environment.OSVersion.Version.Major < 6)
{
return;
}
bool cancelExit = true;
try
{
List<Form> openForms = Application.OpenForms.OfType<Form>().ToList();
for (int i = openForms.Count - 1; i >= 0; i--)
{
Form f = openForms[i];
if (f.InvokeRequired)
{
f.Invoke(new MethodInvoker(() =>
{
f.FormClosing += (sender, args) => cancelExit = args.Cancel;
f.Close();
}));
}
else
{
f.FormClosing += (sender, args) => cancelExit = args.Cancel;
f.Close();
}
if (cancelExit) break;
}
if (cancelExit) return;
Process.Start(new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Arguments = commandLine.Length > 0 ? string.Join(" ", commandLine) : string.Empty
});
Application.Exit();
}
finally
{
_exiting = false;
}
}
}
This worked for me:
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Application.Current.Shutdown();
Some of the other answers have neat things like waiting for a ping to give the initial application time to wind down, but if you just need something simple, this is nice.
For .Net application solution looks like this:
System.Web.HttpRuntime.UnloadAppDomain()
I used this to restart my web application after changing AppSettings in myconfig file.
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
configuration.AppSettings.Settings["SiteMode"].Value = model.SiteMode.ToString();
configuration.Save();
I am calling an external command line app that continously spits out information into the console window. I'd like to read the information and pass it into my code in order to report progress.
But... I never get any values back at all. If I use sr.ReadToEnd(), it gets stuck until the app closes and just an empty string comes back. What do I need to do to read the text in the command line window of the external app correctly?
Here is my code for the test, doesn't have to be threaded, the stream comes back empty no matter what I seem to do:
private void runApp(string args, string app)
{
ProcessStartInfo pInfo = new ProcessStartInfo(app, args);
pInfo.CreateNoWindow = true;
pInfo.RedirectStandardOutput = true;
pInfo.UseShellExecute = false;
Thread t = new Thread(getProgress);
t.Start();
p = Process.Start(pInfo);
p.WaitForExit();
p.Close();
}
private void getProgress()
{
StreamReader sr = p.StandardOutput;
//Get's stuck here until the app closes, nothing is ever outputted
string output = sr.ReadLine();
//Just for testing, debugging here
while (true)
{
Console.WriteLine(output);
System.Threading.Thread.Sleep(1000);
}
sr.Close();
}
I'm thinking that the thread is started before the process and somehow you are deadlocking. Check out http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
I see this subject is asked again and again every few days... see my answer here:
Running a c++ console app inside a C# console app