The pace of change with .Net is pretty relentless these days. And every so often they ship a thing that fixes a challenge I've been mulling over addressing myself. That's happened again with Process API changes in preview 4 of .Net 11. So if you have code which spawns other executables, you might want to look at what they've changed...
url copied!
The tool I use for writing these blog posts is a windows app I created to be a basic text editor with some features to automate writing tasks I need to do regularly. To preview the posts I'm writing the editor executes my Statiq site builder cli-tool in a separate process. So each time I save changes to my writing, that tool detects the modification and causes the local test site to update so I can view an up-to-date version of the page in a browser.
But the automation that lets me click one button in my editor to fire up the preview server has come with a minor irritiation: Until now, if I quit the editor program it hasn't automatically quit the preview process at the same time. And while that preview code is running it has a lock on a bunch of files in its
bin
folder. So I can't do things like publish the site or run a build of the overall project until I've manually stopped the preview process. When I forget to close everything down properly I end up with errors like this from MSBuild:
C:\Program Files\Microsoft Visual Studio\18\Insiders\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(5398,5): warning MSB3026: Could not copy "<myproject>\obj\Debug\net11.0-windows10.0.17763.0\apphost.exe" to "bin\Debug\net11.0-windows10.0.17763.0\StatiqEditor.exe". Beginning retry 10 in 1000ms. The process cannot access the file '<myproject>\bin\Debug\net11.0-windows10.0.17763.0\StatiqEditor.exe' because it is being used by another process. The file is locked by: "StatiqEditor (29188)"
And it sits there retrying for a while before eventually deciding to give up.
It's not really a big problem - I've ended up with "stop all the processes" muscle memory as a result - but it's certainly caused me a bit of frustration when I've forgotten.
url copied!
Before this new release, I could have solved the issue with some Platform Invoke business to call the relevant Windows APIs to do this manually. But working out how to do that was enough hassle that I never got around to implementing it.
But with the Preview 4 release of .Net 11 we have a bunch of changes to the wrappers for system-level Process APIs. There's loads of stuff here,
reworking a lot of behaviour to make spawning and interacting with processes much easier. But the key thing for me is
a new property
on
ProcessStartInfo
with the delightful name
KillOnParentExit. That provides an automated mechanism to do the setup that ensures that when your main process exits, the child processes it spawned can be terminated too.
So I can take the code I had for spawning the Statiq process and one line:
var process = new Process(); process.Exited += PreviewProcess_Exited; process.EnableRaisingEvents = true; process.StartInfo.Arguments = _config.StatiqBuildCommand; process.StartInfo.WorkingDirectory = System.IO.Path.Combine(_userProfile, _config.ProjectFolder); process.StartInfo.FileName = "dotnet"; process.StartInfo.KillOnParentExit = true; process.Start();
And now quitting the editor will mean this process stops too.
Much simpler than having to wire this up manually myself...
↑ Back to top