// For Windows, activate conda and run python command in one cmd instance:
string command = $"conda activate {condaEnvName} && uvicorn ucs:app --host 0.0.0.0 --port 21421";
var processInfo = new ProcessStartInfo("cmd.exe", "/C " + command)
{
RedirectStandardOutput = false,
RedirectStandardError = false,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = @"C:\AI\Whisper"
};
using (var process = new Process())
{
process.StartInfo = processInfo;
process.Start();
}
}
Assistant
To make the BootWhisper method start a child process of your C# app and allow you to interact with it (for example, read output, errors, and possibly send input), you need to configure the ProcessStartInfo properly and read redirected streams.
Here’s an updated version of your code that makes the process a child of your application, redirects standard output and error (so you can capture logs), and does not use shell execute. I also added minimal example of asynchronously reading the output and error streams:
csharp
using System;
using System.Diagnostics;
using System.Threading.Tasks;
public void BootWhisper(string condaEnvName, string pythonScriptName)
{
// Compose the command to activate the conda environment and run the script via uvicorn
string command = $"conda activate {condaEnvName} && uvicorn ucs:app --host 0.0.0.0 --port 21421";
var processInfo = new ProcessStartInfo("cmd.exe", "/C " + command)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = false,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = @"C:\AI\Whisper"
};
var process = new Process
{
StartInfo = processInfo,
EnableRaisingEvents = true
};
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.WriteLine("[OUT] " + e.Data);
// You can also raise events or log here
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
Console.Error.WriteLine("[ERR] " + e.Data);
// You can also raise events or log here
}
};
process.Exited += (sender, e) =>
{
Console.WriteLine("Process exited with code " + process.ExitCode);
process.Dispose();
};
process.Start();
// Begin async reading of output and error
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// Not calling WaitForExit here so the process runs asynchronously.
}
Notes:
UseShellExecute = false makes it a true child process of your application, and lets you redirect streams.
Redirecting StandardOutput & StandardError lets you capture the child's console output.
CreateNoWindow = true means the cmd window will not pop up.
I removed RedirectStandardInput since you didn't use input, but you can set it true if you want to send input.
The example listens to output/error events and logs them to your console, you can replace that with your own logging or UI updates.
The process will stay running asynchronously. You can keep a reference to the process if you want to kill it later.
On Conda activation via cmd:
Inside a single cmd.exe /C command, typical conda activation scripts are batch scripts and environment variables will be available only in that cmd session, so it should work as written, but double-check your environment works correctly when run this way.
If you want to run a standalone python script directly, or if activation is problematic, you could also run conda run -n {condaEnvName} uvicorn ucs:app --host 0.0.0.0 --port 21421 without cmd.exe. Let me know if you want that code style.