У меня есть приложение, которое выдает исключение между потоками, когда я пытаюсь запустить его с его командами.
/// <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](https://i.stack.imgur.com/pDFSL.png)
Полная ошибка: System.ComponentModel.Win32Exception HResult = 0x80004005 Сообщение = Доступ запрещен Source = System StackTrace: at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) в System.Diagnostics.Process.Start () в SoftwareTestMatrixTool.MainForm.RunExe (String exeFile, String commandLine, RichTextBox logArea, Кнопка startButton) в C: \ MYPATH \ MainForm.cs: строка 669 в программной версии.<> c__DisplayClass15_0.b__0 () в C: \ MYPATH \ MainForm.cs: строка 613 в System.Threading.ThreadHelper.ThreadStart_Context (состояние объекта) в System.Threading.ExecutionContext.RunInternal (ExecutionContext executeContext, StateCallback, обратный вызов состояния, обратный вызов ContextCallbackpreserveSyncCtx) в System.Threading.ExecutionContext.Run (обратный вызов ExecutionContext executeContext, обратный вызов ContextCallback, состояние объекта, Boolean preserveSyncCtx) в системном объекте.tate) в System.Threading.ThreadHelper.ThreadStart ()
Ошибка показывает строку вызывающего абонента этой функции.