Har*_*run 0 c# windows-services
我正在使用Windows服务来执行ffmpeg.exe转换视频文件.我是通过在c#.net中查看进程来完成此操作的.问题是
我在Windows服务的OnStart()中的代码如下:
FilArgs = string.Format("-i {0} -ar 22050 -qscale 1 {1}", InputFile, OutputFile);
Process proc;
proc = new Process();
try
{
proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
proc.StartInfo.Arguments = FilArgs;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
eventLog1.WriteEntry("Going to start process of convertion");
proc.Start();
string StdOutVideo = proc.StandardOutput.ReadToEnd();
string StdErrVideo = proc.StandardError.ReadToEnd();
eventLog1.WriteEntry("Convertion Successful");
eventLog1.WriteEntry(StdErrVideo);
}
catch (Exception ex)
{
eventLog1.WriteEntry("Convertion Failed");
eventLog1.WriteEntry(ex.ToString());
}
finally
{
proc.WaitForExit();
proc.Close();
}
Run Code Online (Sandbox Code Playgroud)
当我试图转换大型视频文件(我在文件大小> 14MB时遇到它)如上所述,服务无法启动并处于"启动"状态.
直接通过命令提示符运行时的ffmpeg也适用于较大的文件.
任何人请我如何解决这个问题......
这很糟糕,因为onstart()应该只为工作循环启动后台线程.OnStart()应该立即返回,所以不必在那里耗费时间.
Windows等待OnStart()在超时内返回,如果没有,将终止它.
基本上你在那里开始一个线程:
private Thread _workerThread;
private bool _closing = false;
... OnStart(...)
{
_workerThread = new Thread(new ThreadStart(Work));
_workerThread .Start();
}
private void Work()
{
while(!_closing)
{
// do the processing if there is work otherwise sleep for say 10 seconds
}
}
... OnStop(...)
{
_closing = true;
_workerThread.Abort()
}
Run Code Online (Sandbox Code Playgroud)