【问题标题】:This operation requires an interactive window station此操作需要交互式窗口站
【发布时间】:2016-03-08 04:49:12
【问题描述】:

我在尝试从计划任务调用的程序中调用程序时收到此错误。

 private static void CallProgram(int jobID, int productID)
    {
        DataTable dtProduct = GetProductInformation(productID);

        try
        {
            // 1
            // Initialize process information.
            //
            ProcessStartInfo p = new ProcessStartInfo();
            p.WorkingDirectory = string.Format(@"{0}", dtProduct.Rows[0]["ProgramDirectory"].ToString());
            p.FileName = dtProduct.Rows[0]["ProgramName"].ToString();
            // 2
            // add arguements
            string[] args = { jobID.ToString() };
            p.Arguments = String.Join(" ", args);
            p.UseShellExecute = true;
            p.Verb = "runas";
             //p.WindowStyle = ProcessWindowStyle.Hidden;
            p.CreateNoWindow = true;
            // 3.
            // Start process and wait for it to exit
            //
            Process x = Process.Start(p);
            //x.WaitForExit();


        }
        catch (Exception ex)
        {
            string function = MethodBase.GetCurrentMethod().Name;
            ErrorHandling(function, ex.Message, ex.StackTrace);
            throw;
        }
    }

我目前有一个执行此操作的 Windows 服务。但是我发现它有时会挂起。我已经构建了一个控制台应用程序,该应用程序的功能与该服务的功能完全相同。我的想法是将它放在任务调度程序上,以便它按设定的时间间隔运行。类似于windows服务。但是,当我从调度程序运行程序时,我收到以下错误

WorkOnQueue 错误:此操作需要交互式窗口 站堆栈跟踪:在 System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) 在 System.Diagnostics.Process.Start(ProcessStartInfo startInfo) 在 RenkimAutomation.Program.CallProgram(Int32 jobID, Int32 产品 ID)在 RenkimAutomation.Program.WorkOnQueue() 机器名称: 产品服务时间戳:2016 年 3 月 7 日下午 6:11:04

我不知道如何解决这个错误。请帮忙

【问题讨论】:

  • 你可以尝试在管理员模式下运行你的应用程序
  • 计划任务设置为以最高权限运行。
  • 我现在无法验证它,但我认为WindowStyle 不是您想要的ProcessStartInfo 上的参数。查看CreateNoWindow
  • @object88 我修改了程序以使用 CreateNoWindow = true。但是我仍然收到错误。

标签: c# console-application


【解决方案1】:

您需要从代码中删除“p.UseShellExecute = true;”,因为它需要用户手动选择“是”或“否”才能以管理员身份执行。相反,在任务计划程序中,选择执行用户作为管理员,然后在您的代码中使用以下命令以管理员身份执行流程:p.Verb = "runas";

所以修改后的代码如下:

private static void CallProgram(int jobID, int productID)
{
    DataTable dtProduct = GetProductInformation(productID);

    try
    {
        // 1
        // Initialize process information.
        //
        ProcessStartInfo p = new ProcessStartInfo();
        p.WorkingDirectory = string.Format(@"{0}", dtProduct.Rows[0]["ProgramDirectory"].ToString());
        p.FileName = dtProduct.Rows[0]["ProgramName"].ToString();
        // 2
        // add arguements
        string[] args = { jobID.ToString() };
        p.Arguments = String.Join(" ", args);
        p.Verb = "runas";
         //p.WindowStyle = ProcessWindowStyle.Hidden;
        p.CreateNoWindow = true;
        // 3.
        // Start process and wait for it to exit
        //
        Process x = Process.Start(p);
        //x.WaitForExit();


    }
    catch (Exception ex)
    {
        string function = MethodBase.GetCurrentMethod().Name;
        ErrorHandling(function, ex.Message, ex.StackTrace);
        throw;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 2010-12-06
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多