我目前正在开发一个实现了fileSystemWatcher的Windows服务。视频被上传到一个文件夹中,在这个文件夹中,filewatcher如下触发创建的事件来转换视频。如果进程崩溃/挂起,请避免锁定服务
private void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (ConvertVideo(e.FullPath, e.Name))
{
WriteToEventLog(String.Format("Successfully converted video - {0}", e.FullPath), EventLogEntryType.Information);
}
}
在ConvertVideo
创建一个新的过程,但我遇到了在那里的进程崩溃/挂起/消失问题,它出现在主线程,然后锁定为等待WaitForExit()
有效地崩溃的服务,因为没有其他然后可以转换视频。如果进程死亡,我怎么能避免锁定整个服务?
private bool ConvertVideo(string SourcePath, string Filename)
{
try
{
// Create new process
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "C:\Handbrake\HandBrakeCLI.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = GetArguments(SourcePath, Filename);
int? exitCode = null;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
exitCode = exeProcess.ExitCode;
}
}
catch(Exception ex)
{
return false;
}
}
注:代码被缩短这个例子
任何带有完整源代码的最终解决方案? – Kiquenet