Process proc=null;//declaration at top of windows form code
proc = Process.Start(imagefilename);
if (proc != null) proc.Kill();
but proc is always null.
if I use proc = new Process(); proc.StartInfo.FileName=imagefilename; proc.Start(); proc.Kill()//after image showing. gives no process associate error. Help! I am using VS 2012 Express because I have to use and create Windows Forms and newer version don't support that well or at all.

Tuhin PaulRank #7 · 71.9K reputationPosted Mar 31, 2023, 1:33 PM EDT
ProcessStartInfo startInfo = new ProcessStartInfo(imagefilename); Process imageProcess = Process.Start(startInfo); if (imageProcess != null) { imageProcess.WaitForExit(); // wait for the process to exit imageProcess.Dispose(); // dispose of the process resources }we use ProcessStartInfo to create a new process and specify the file name of the image to be displayed. Then we start the process using Process.Start() and store the returned Process object in a variable imageProcess.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 31, 2023, 1:31 PM EDT
To kill a process that displays an image file, you first need to start the process using
Process.Start()and then obtain the process handle to kill it.Jon Jacobs16 reputationPosted Mar 31, 2023, 5:33 PM EDT
Thank you.
Process proc = Process.Start(startInfo); ALWAYS returns null for me.
So I will experiment with:
ProcessStartInfo startinfo = new ProcessStartInfo(imagefilename);
proc = new Process();
proc.StartInfo = startinfo;
proc.Start();
handle = proc.Handle;
Jon Jacobs16 reputationPosted Mar 31, 2023, 5:39 PM EDT
How do I kill a process by its handle? the Kill method doesn't take parameters.
Jon Jacobs16 reputationPosted Mar 31, 2023, 5:49 PM EDT
Whenever I use proc.Kill(); I get: "No process is associated with this object."
I think I get what is happening. As soon as the Windows Photo Viewer is launched by proc.Start, the process immediately exits. So what I really need is a way to find the Windows Photo Viewer instance and kill it. In a run of my program, I will have launched dozens of images, so I need to be able to kill the current one before launching the next one.
Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 1, 2023, 12:58 AM EDT
Try below,
Process process = Process.Start("imagefilename.jpg"); if (process != null) { process.WaitForInputIdle(); // wait for the process to become idle process.Kill(); // kill the process }Thanks