【问题标题】:Starting a process not working in my windows service启动一个在我的 Windows 服务中不起作用的进程
【发布时间】:2010-11-23 10:14:45
【问题描述】:

我正在尝试使用进程通过 c#.net 执行 exe 文件。执行失败,返回以下异常:

System.InvalidOperationException:没有进程与此对象关联。 在 System.Diagnostics.Process.EnsureState(状态状态) 在 System.Diagnostics.Process.EnsureState(状态状态) 在 System.Diagnostics.Process.GetProcessHandle(Int32 访问,布尔 throwIfExited) 在 System.Diagnostics.Process.WaitForExit(Int32 毫秒) 在 System.Diagnostics.Process.WaitForExit() 在 VideoHandlingWinService.VideoHandlingService.ConvertVideoToFlv(字符串 SavePath,字符串 WithOutExt,字符串 InputFile,字符串 spath,Int32 VideoQueueId) 在 VideoHandlingWinService.VideoHandlingService.VideoHandling(字符串 VideoName,字符串 SavePath,字符串 InputFile,字符串 WithOutExt,字符串 spath,Int32 VideoQueueId,字符串 VideoDescription,Int32 RegisteredUserId,Int32 CategoryId,字符串 VideoTitle) 在 VideoHandlingWinService.VideoHandlingService.StartHandlingVideo() 在 VideoHandlingWinService.VideoHandlingService.OnStart(String[] args)

我的启动过程代码如下:

Process proc = new Process();
string spath = AppDomain.CurrentDomain.BaseDirectory.ToString();

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;

    proc.Start();

    string StdOutVideo = proc.StandardOutput.ReadToEnd();
    string StdErrVideo = proc.StandardError.ReadToEnd();             
}
catch { }
finally
{
    proc.WaitForExit();
    proc.Close();
}

请告诉我如何在 Windows 服务中执行此操作。我也以本地帐户运行 Windows 服务,希望 exe 没有权限问题。

【问题讨论】:

  • 你有一个空的 catch 块。删除它或在其中添加一些日志记录,以便您了解实际情况。
  • 可能是你的空catch吞了一个异常,进程从未执行过,解释错误信息...

标签: c# windows-services process


【解决方案1】:

无论如何使用System.IO.Path.Combine(path, "ffmpeg\\ffmpeg.exe")

【讨论】:

  • 异常是由于我的路径。对于 Windows 服务,我不能使用根路径,因为它在外面工作。现在我通过提供 spath 的完整物理路径来摆脱异常。
【解决方案2】:

我认为错误发生在

proc.WaitForExit();

你确定ffmpeg没有被执行?

根据 MSDN

WaitForExit()()() 重载是 用于制作当前线程 等到相关进程 终止。该方法指示 进程组件等待无限 该过程的时间量和 要退出的事件处理程序。

我猜你在finally()的时候ffmpeg已经退出了。

【讨论】:

  • put proc.WaitForExit(); proc.Close();在 proc.start() 之后并检查 hppens
  • 异常是由于我的路径。对于 Windows 服务,我不能使用根路径,因为它在外面工作。现在我通过提供 spath 的完整物理路径来摆脱异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多