C#表单应用程序Process.Start()提供访问被拒绝错误

时间:2019-06-14 10:50:17

标签: c# winforms

我有一个应用程序,当我尝试使用其命令运行它时,该应用程序会提供跨线程异常。

    /// <summary>
    /// to run the EXE file in the c# code
    /// </summary>
    /// <param name="exeFile">the exe file path and name</param>
    /// <param name="commandLine"></param>
    /// <param name="logArea"></param>
    /// <param name="startButton"></param>
    private void RunExe(string exeFile, string commandLine, RichTextBox logArea, Button startButton)
    {
        process = new Process
        {
            StartInfo =
            {
                FileName = exeFile,
                Arguments = commandLine,
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            }
        };
        //process instant
        //shell name
        //adding command line as argument to the shell
        //listen to the outputs - non error stream
        //listen to the outputs - error stream
        process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            if (!string.IsNullOrEmpty(e.Data))
            {
                logArea.Invoke(new Action(() =>
                {
                    AppendLine(e.Data, logArea);
                    Application.DoEvents();
                }));
            }
        });

        process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            logArea.Invoke(new Action(() =>
            {
                AppendLine(e.Data, logArea);
                Application.DoEvents();
            }));
        });

        Cursor.Current = Cursors.WaitCursor;
        startButton.Invoke(new Action(() => startButton.Enabled = false));

        try
        {
            process.Start(); //start the process instant
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
        }
        finally
        {
            Cursor.Current = Cursors.Default;
            startButton.Invoke(new Action(() => startButton.Enabled = true));
            GenerateButtonEnable(true);
        }

        process.Close();

        if (startButton == buttonGenerateUT)
        {
            if (UTMatrixStopButton.InvokeRequired)
            {
                UTMatrixStopButton.Invoke(new MethodInvoker(delegate
                {
                    UTMatrixStopButton.Enabled = false;
                }));
            }
            else
            {
                UTMatrixStopButton.Enabled = false;
            }
        }
        else
        {   // invokes required when cross thread tries to reach the ui
            if (IRStopButton.InvokeRequired)
            {
                IRStopButton.Invoke(new MethodInvoker(delegate
                {
                    IRStopButton.Enabled = false;
                }));
            }
            else
            {
                IRStopButton.Enabled = false;
            }
        }
    }

这是我启动流程Process.Start时的功能,出现以下错误。有趣的是,当我将其用作应用程序时,一切正常。当我尝试在VS中调试它时,会出现此错误。

enter image description here

完全错误:System.ComponentModel.Win32Exception HResult = 0x80004005消息=拒绝访问源=系统StackTrace:在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)在System.Diagnostics.Process.Start()在SoftwareTestMatrixTool.MainForm .RunExe(String exeFile,String commandLine,RichTextBox logArea,Button startButton)在SoftwareTestMatrixTool.MainForm上的C:\ MYPATH \ MainForm.cs:line 669。<> c:\ C__DisplayClass15_0.b__0()在C:\ MYPATH \ MainForm.cs:line 613在System.Threading.ExecutionContext.RunInternal(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔状态保持布尔值(Sync状态)在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象处)状态,在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态)处在System.Threading.ThreadHelper.Th readStart()

错误显示此函数的调用方行。

0 个答案:

没有答案