poocess问题从c#开始 [英] problem with poocess Start in c#
问题描述
嗨
请在下面的代码中找一段时间它工作正常但有时候它退出以下的程序是代码
< pre lang =c#> private void backgroundWorker1_DoWork( object sender,DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
string 结果;
ProcessStartInfo start = new ProcessStartInfo();
string myExeDir =( new FileInfo(System.Reflection.Assembly.GetEntryAssembly()。Location ))。目录+ @ \ + Python.exe;
start.FileName = myExeDir;
start.Arguments =(Path.GetDirectoryName(Assembly.GetExecutingAssembly()。Location)+ @ \ + TA + @ \ + 工具 + @ \ + strSelectedTreeNade + 。py);
start.ErrorDialog = false ;
start.UseShellExecute = false ;
start.RedirectStandardError = true ;
start.RedirectStandardInput = true ;
start.RedirectStandardOutput = true ;
start.CreateNoWindow = true ;
StreamReader reader = null ;
// listLog.Items.Clear();
listLog.Invoke( new 操作(()= > listLog.Items.Clear()));
// 读取文件
string str_TestSuiteName1 = strSelectedTreeNade;
List< string> m_CombinedTestsuiteNames = new List< string>();
使用(处理流程= Process.Start(开始))
{
使用(reader = process.StandardOutput)
{
// ...
}
}
}
proces.StandardOutput成为空...
- 添加预标签以获得正确的代码格式
- 替换\\
与 @\
等效,以便字符串分隔符不会搞砸
[/ Edit]
我不确定,但您的错误可能与此行有关:
引用:使用(reader = process.StandardOutput)
'使用
'将尝试处置process.StandardOutput
在封闭的大括号。但是,我希望它是'Process
'的责任。
请参阅以下示例。你可以根据你的代码进行调整。
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;
使用System.Diagnostics;使用System.Drawing
;
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Test_ProcStart
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent() ;
InitOthers();
}
private void InitOthers()
{
this.bg.DoWork + = bg_DoWork;
this.textBox_Input.Text = @cmd / C dir d:\;
}
void bg_DoWork(对象发送者,DoWorkEventArgs e)
{
string a = e.Argument.ToString();
string fn = a.Split('')。first();
string args = a.Substring(a.IndexOf('')+ 1);
// WORKS WELL
ProcessStartInfo si = new ProcessStartInfo()
{
FileName = fn,
Arguments = args,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = false,
RedirectStandardOutput = true,
UseShellExecute = false
};
使用(处理p =新处理())
{
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);
}
}
}
hi
please fint the Below Code some time it work fine but some time it Exit the Proce below is the Code
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
string result;
ProcessStartInfo start = new ProcessStartInfo();
string myExeDir = (new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location)).Directory + @"\" + "Python.exe";
start.FileName = myExeDir;
start.Arguments = (Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + "TA" + @"\" + "Tool" + @"\" + strSelectedTreeNade + ".py");
start.ErrorDialog = false;
start.UseShellExecute = false;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.RedirectStandardOutput = true;
start.CreateNoWindow = true;
StreamReader reader = null;
//listLog.Items.Clear();
listLog.Invoke(new Action(() => listLog.Items.Clear()));
// reading the file
string str_TestSuiteName1 = strSelectedTreeNade;
List<string> m_CombinedTestsuiteNames = new List<string>();
using (Process process = Process.Start(start))
{
using (reader = process.StandardOutput)
{
// ...
}
}
}
the proces.StandardOutput Become Null..
[Edit]
- Added pre tag to have correct code formatting
- Replaced "\\"
with @"\"
equivalent so that string delimiters are not messed up
[/Edit]
I'm not sure but your error may be related to this line:
Quote:using (reader = process.StandardOutput)
'using
' will try to disposeprocess.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); // WORKS WELL 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); } } }
这篇关于poocess问题从c#开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!