FYI, I’ve ported my code from a C# component to a plug-in, which created some issues with the WaitHandle.
The below solution is probably better anyway.
doc.NewSolution(true, GH_SolutionMode.CommandLine);
while (doc.SolutionState != GH_ProcessStep.PostProcess) { }