C
C#2mo ago
"Mes"vin

Node process not being killed properly when WPF closes

I have a client and a server process running in the background that's being spawned using C# in WPF but they're not being killed when I want them too.
1 Reply
"Mes"vin
"Mes"vinOP2mo ago
C#
public void MainWindow_Closed(object sender, EventArgs e)
{
try
{
Console.WriteLine("Closing application...");

// Kill all child processes
foreach (var childProcess in _ChildProcesses)
{
Console.WriteLine($"Killing child process with PID: {childProcess.Id}");
if (!childProcess.HasExited)
{
childProcess.Kill();
childProcess.WaitForExit(); // Ensure it exits
}
}

// Kill the main client process
if (_Client != null && !_Client.HasExited)
{
Console.WriteLine($"Killing client process with PID: {_Client.Id}");
_Client.Kill();
_Client.WaitForExit();
}

// Kill the main server process
if (_Server != null && !_Server.HasExited)
{
Console.WriteLine($"Killing server process with PID: {_Server.Id}");
_Server.Kill();
_Server.WaitForExit(); // Ensure the server process exits
}

// Graceful shutdown
Application.Current.Shutdown();
}
catch (Exception ex)
{
MessageBox.Show($"Error while closing: {ex.Message}");
}
}
C#
public void MainWindow_Closed(object sender, EventArgs e)
{
try
{
Console.WriteLine("Closing application...");

// Kill all child processes
foreach (var childProcess in _ChildProcesses)
{
Console.WriteLine($"Killing child process with PID: {childProcess.Id}");
if (!childProcess.HasExited)
{
childProcess.Kill();
childProcess.WaitForExit(); // Ensure it exits
}
}

// Kill the main client process
if (_Client != null && !_Client.HasExited)
{
Console.WriteLine($"Killing client process with PID: {_Client.Id}");
_Client.Kill();
_Client.WaitForExit();
}

// Kill the main server process
if (_Server != null && !_Server.HasExited)
{
Console.WriteLine($"Killing server process with PID: {_Server.Id}");
_Server.Kill();
_Server.WaitForExit(); // Ensure the server process exits
}

// Graceful shutdown
Application.Current.Shutdown();
}
catch (Exception ex)
{
MessageBox.Show($"Error while closing: {ex.Message}");
}
}
c#
private Process _Client;
private Process _Server;
private List<Process> _ChildProcesses = new List<Process>();

public MainWindow()
{
InitializeComponent();

// Path to MonacoLuau folder
string monacoLuauPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MonacoLuau");
string nodeModulesPath = System.IO.Path.Combine(monacoLuauPath, "node_modules");

// Check if node_modules exists
if (Directory.Exists(nodeModulesPath))
{
string clientPath = System.IO.Path.Combine(monacoLuauPath, "Client");
string serverPath = System.IO.Path.Combine(monacoLuauPath, "Server");

_Client = StartProcess(clientPath, "run dev");
_Server = StartProcess(serverPath, "run dev");

// Track child processes of server and client
_ChildProcesses.AddRange(GetChildProcesses(_Client));
_ChildProcesses.AddRange(GetChildProcesses(_Server));
}
else
{
// Handle npm install logic
RunNpmInstall(monacoLuauPath);
}
}
c#
private Process _Client;
private Process _Server;
private List<Process> _ChildProcesses = new List<Process>();

public MainWindow()
{
InitializeComponent();

// Path to MonacoLuau folder
string monacoLuauPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MonacoLuau");
string nodeModulesPath = System.IO.Path.Combine(monacoLuauPath, "node_modules");

// Check if node_modules exists
if (Directory.Exists(nodeModulesPath))
{
string clientPath = System.IO.Path.Combine(monacoLuauPath, "Client");
string serverPath = System.IO.Path.Combine(monacoLuauPath, "Server");

_Client = StartProcess(clientPath, "run dev");
_Server = StartProcess(serverPath, "run dev");

// Track child processes of server and client
_ChildProcesses.AddRange(GetChildProcesses(_Client));
_ChildProcesses.AddRange(GetChildProcesses(_Server));
}
else
{
// Handle npm install logic
RunNpmInstall(monacoLuauPath);
}
}
Want results from more Discord servers?
Add your server