我做了一个C#后台服务程序,大根的功能就是做文档转码我的代码中有这样一断  Process pc = new Process();
                            ProcessStartInfo psi = new ProcessStartInfo(swfToolPath, outputFilePath + " " + outputSwfFilePath);
                            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                            pc.StartInfo = psi;
                                                        pc.Start();
                            pc.WaitForExit();
                            WriteFiles(string.Format("文档:" + dt.Rows[i]["fileName"].ToString() + "转码成功!当前时间{0}", DateTime.Now.ToLongTimeString() + "/n"));
                            SqlOperator("update tb_files set  swfUrl='1'  where ID=" + dt.Rows[i]["ID"].ToString());
代码到这里就不执行了
我想问是不是后台服务程序不能用Process?
如果可以我该怎么写
如果不可以,那后台服务程序如果调用一个.exe文件呢?

解决方案 »

  1.   

    在哪里不执行了?你是在一个windows service里去调别的进程?
      

  2.   


    Process pc = new Process();
    是在windows service里调用别的进程,这样可以吗??
      

  3.   

    你的"别的进程"会弹出对话框吗?需要等待用户输入/介入吗?
    如果是,pc.WaitForExit()将永远等不到进程结束。
      

  4.   

    WaitForExit() 重载用于使当前线程处于等待状态,直到关联的进程终止。 This method instructs the Process component to wait an infinite amount of time for the process and event handlers to exit. 这可能会导致应用程序停止响应。 例如,如果对有用户界面的进程调用 CloseMainWindow,并且该进程被编写为从不进入其消息循环,则可能不处理向操作系统发出的终止关联进程的请求。  注意  
    In the .NET Framework 3.5 版 and earlier versions, the WaitForExit() overload waited for MaxValue milliseconds (approximately 24 days), not indefinitely. Also, previous versions did not wait for the event handlers to exit if the full MaxValue time was reached. 
     此重载确保所有处理均已完成,包括重定向标准输出的异步事件处理。 当标准输出重定向到异步事件处理程序时,您应该在调用 WaitForExit(Int32) 重载之后使用此重载。 当关联的进程退出时(即由操作系统通过正常或异常终止关闭该进程时),系统将存储关于该进程的管理信息并返回到已调用 WaitForExit() 的组件。 然后 Process 组件可使用已退出进程的 Handle 访问这些信息,其中包括 ExitTime。 因为关联进程已经退出,所以该组件的 Handle 属性不再指向现有进程资源。 而是,该句柄仅可用于访问关于该进程资源的操作系统信息。 系统知道已退出进程的句柄尚未由 Process 组件释放,所以它在内存中保存 ExitTime 和 Handle 信息,直到 Process 组件明确释放这些资源。 为此,只要为 Process 实例调用 Start,则请在关联进程已经终止而且不再需要任何关于它的管理信息时,调用 Close。 Close 将释放分配给已退出进程的内存。 
      

  5.   

    尚未设置进程 Id,而且不存在可从其确定 Id 属性的 Handle。 - 或 - 没有与此 Process 对象关联的进程。 - 或 - 您正尝试为远程计算机上运行的进程调用 WaitForExit()。 此方法仅对本地计算机上运行的进程可用。 
     
      

  6.   

    用事件试试
    pc.Exited += new EventHandler(object sender, EventArgs e)
    {
        WriteFiles(string.Format("文档:" + dt.Rows[i]["fileName"].ToString() + "转码成功!当前时间{0}", DateTime.Now.ToLongTimeString() + "/n"));
        SqlOperator("update tb_files set  swfUrl='1'  where ID=" + dt.Rows[i]["ID"].ToString());
    });去掉pc.WaitForExit();
      

  7.   

    先注册结束事件再启动pc.Start();
      

  8.   

    ProcessStartInfo psi = new ProcessStartInfo(swfToolPath, outputFilePath + " " + outputSwfFilePath);
    注意也可能是路径问题,使用绝对路径试下
      

  9.   

    Process pc = new Process();
    ProcessStartInfo psi = new ProcessStartInfo(swfToolPath, outputFilePath + " " + outputSwfFilePath);
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    pc.StartInfo = psi
    pc.Exited += new EventHandler((object sender, EventArgs e)
    =>
    {
        WriteFiles(string.Format("文档:" + dt.Rows[i]["fileName"].ToString() + "转码成功!当前时间{0}", DateTime.Now.ToLongTimeString() + "/n"));
        SqlOperator("update tb_files set  swfUrl='1'  where ID=" + dt.Rows[i]["ID"].ToString());
    });
    pc.Start();
      

  10.   

    路径是绝对路径,而且在独立的winform程序下是可以成功执行的