Skip to content

Commit

Permalink
Trying dispose approach
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia committed Oct 21, 2024
1 parent cef2e0c commit dbd472c
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions DCL_PiXYZ/Utils/NPMUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public static void DoNPMInstall(string sceneManifestProjectDirectory)

public static async Task<string> RunNPMTool(string sceneManifestProjectDirectory, string sceneType, string sceneValue)
{
//TODO: I need standard output for it to work. Why?
var process = new Process()
string firstErrorLine = "";
using (var process = new Process())
{
StartInfo = new ProcessStartInfo
//TODO: I need standard output for it to work. Why?
process.StartInfo = new ProcessStartInfo
{
FileName = "powershell", // or the full path to npm if not in PATH
Arguments = $"npm run start --{sceneType}={sceneValue}", // replace with your npm command
Expand All @@ -72,13 +73,9 @@ public static async Task<string> RunNPMTool(string sceneManifestProjectDirectory
RedirectStandardOutput = true, // if you want to read output
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};

string firstErrorLine = "";
using (process)
{
};
process.EnableRaisingEvents = true;

process.OutputDataReceived += (sender, args) =>
{
FileWriter.WriteToConsole($"NPM OUTPUT: {args.Data}");
Expand All @@ -94,21 +91,25 @@ public static async Task<string> RunNPMTool(string sceneManifestProjectDirectory

process.BeginOutputReadLine();
process.BeginErrorReadLine();

await Task.Run(() =>
{

//Timeout is based on the current frame rate (90 frames * 33ms)
//No scene should take longer than 2970ms to finish running frame,
//but all the setup may take time. So we are leaving a big and safe margin
if (!process.WaitForExit(20_000))
bool exited = await Task.Run(() => process.WaitForExit(20_000));

// If process hasn't exited within the timeout, kill it
if (!exited)
{
process.Kill();
});
}
else
{
process.WaitForExit(); // ensure all async output is finished
}

if (!string.IsNullOrEmpty(firstErrorLine))
return firstErrorLine;
}

return "";
return !string.IsNullOrEmpty(firstErrorLine) ? firstErrorLine : "";
}
}
}
}

0 comments on commit dbd472c

Please sign in to comment.