I'm not sure but your error may be related to this line:
Quote:
using (reader = process.StandardOutput)
'
using
' will try to dispose
process.StandardOutput
at the enclosing brace. But, I expect that it is the responsibility of '
Process
'.
See the following example. You can adapt it to your code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_ProcStart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitOthers();
}
private void InitOthers()
{
this.bg.DoWork += bg_DoWork;
this.textBox_Input.Text = @"cmd /C dir d:\";
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
string a = e.Argument.ToString();
string fn = a.Split(' ').First();
string args = a.Substring(a.IndexOf(' ') + 1);
ProcessStartInfo si = new ProcessStartInfo()
{
FileName = fn,
Arguments = args,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = false,
RedirectStandardOutput = true,
UseShellExecute = false
};
using (Process p = new Process())
{
p.StartInfo = si;
p.Start();
this.Invoke(new Action(() =>
{
this.textBox_Output.Text = p.StandardOutput.ReadToEnd();
}));
this.Invoke(new Action(() =>
{
this.textBox_Error.Text = p.StandardError.ReadToEnd();
}));
}
}
BackgroundWorker bg = new BackgroundWorker();
private void button_Run_Click(object sender, EventArgs e)
{
bg.RunWorkerAsync(this.textBox_Input.Text);
}
}
}