Note that there are some explanatory texts on larger screens.

plurals
  1. POCleanly killing a console application from within a group of console applications
    primarykey
    data
    text
    <p>I have a windows form that creates multiple console applications of the same program (it calls program.exe any number of times) using </p> <pre><code>process.start(). </code></pre> <p>I am looking for a way to identify a specific running of the application and kill it cleanly (ie cleanly close the 5th process of program.exe but leave all other processes running). I am able to identify each different process of program.exe through ids that were given when the processes were started, but I am unable to close the application any way other than calling</p> <pre><code>process.kill() </code></pre> <p>which does not perform a clean close of the application. </p> <p>I believe that I cannot use </p> <pre><code>process.CloseMainWindow() </code></pre> <p>since the application does not have a window (it is run through a console that runs in the background). I am looking to kill the processes by clicking a button in my GUI after I select the process to be killed from a list of the processes that are running.</p> <p>The reason why I need this is because I need to close all threads and outstanding aspects of each process before it closes.</p> <p>I define each new process as follows,</p> <pre><code>Process process = new Process(); process.StartInfo = info; ExecutionDetails details = new ExecutionDetails(run_id, process, info, session_type, strategy_type, name, ExecutionViewModel); lock (_runs) _runs.Add(run_id, details); // will throw on Add if duplicate id, prevent duplicate ExecutionViewModel.NewRun(details); // add to the view model process.Start(); </code></pre> <p>Where run_id is a GUID that identifies each process.</p> <p>In a seperate class I have the code that is executed through the process which is only referenced through the command prompt that starts the program (ie calls program, provides config variables, etc). </p> <p>Is there any way that I can close the processes cleanly? I was thinking that having an event that is called when I want to kill the process might work but so far I have not been able to get that idea to work since I am unable to specify which process I want closed.</p> <p>** EDIT - My code for the event handling that I have tried to impliment but it is not working. </p> <p>Code In Main Window</p> <pre><code> public void KillOne() { foreach (var details in _runs.Values) { if(details.IsSelected) { StrategyStateManager.SessionClosed(this, details.RunId); } } } </code></pre> <p>Code in StrategyStateManager (Middle class used to hold variables and events to use in program)</p> <pre><code>public delegate void StrategyCloseEventHandler(object sender, StrategyCloseEventArgs e); public static void SessionClosed(object sender, Guid id) { if(CloseSession != null) CloseSession(sender, new StrategyCloseEventArgs(id)); } public class StrategyCloseEventArgs : EventArgs { private readonly Guid id; public StrategyCloseEventArgs(Guid run_id) { id = run_id; } public Guid GetRunID() { return id; } } </code></pre> <p>Code in process that is being started by main window</p> <pre><code> StrategyStateManager.CloseSession += (closeStrategy); void closeStrategy(object sender, StrategyCloseEventArgs e) { if (e.GetRunID() == run_id) { strategy.cleanupForShutdown(); DBSaverSimple.shutdownAll(); logger.Warn("Simulation run stopped by user"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload