Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions src/ImageProcessor.Web.Plugins.PostProcessor/PostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,50 +100,42 @@ private static PostProcessingResultEventArgs RunProcess(string sourceFile, long
return null;
}

int elapsedTime = 0;
bool eventHandled = false;
Process process = null;
try
{
process = new Process
using (var processingFinished = new ManualResetEventSlim(false))
{
Process process = null;
try
{
StartInfo = start,
EnableRaisingEvents = true
};
process = new Process
{
StartInfo = start,
EnableRaisingEvents = true
};

process.Exited += (sender, args) =>
{
result = new PostProcessingResultEventArgs(sourceFile, length);
process?.Dispose();
eventHandled = true;
};
process.Exited += (sender, args) =>
{
result = new PostProcessingResultEventArgs(sourceFile, length);
process?.Dispose();
processingFinished.Set();
};

process.Start();
process.Start();

// Wait for Exited event, but not more than 5 seconds.
const int sleepAmount = 100;
while (!eventHandled)
// Wait for processing to finish, but not more than 5 seconds.
const int MaxWaitTimeMs = 5000;
processingFinished.Wait(MaxWaitTimeMs);
}
catch (System.ComponentModel.Win32Exception ex)
{
elapsedTime += sleepAmount;
if (elapsedTime > 5000)
{
break;
}
// Some security policies don't allow execution of programs in this way
ImageProcessorBootstrapper.Instance.Logger.Log(typeof(PostProcessor), ex.Message);

Thread.Sleep(sleepAmount);
return null;
}
finally
{
// Make sure we always dispose and release
process?.Dispose();
}
}
catch (System.ComponentModel.Win32Exception ex)
{
// Some security policies don't allow execution of programs in this way
ImageProcessorBootstrapper.Instance.Logger.Log(typeof(PostProcessor), ex.Message);

return null;
}
finally
{
// Make sure we always dispose and release
process?.Dispose();
}

return result;
Expand Down