111,129
社区成员
发帖
与我相关
我的任务
分享 //定义委托
public delegate void delegateReadoutput(string result);
public delegate void delegateErroroutput(string result);
public partial class LogInfo : Form
{
//事件
public event delegateReadoutput Readoutput;
public event delegateErroroutput Erroroutput;
public LogInfo(string ExProgram, string ExCommand)
{
InitializeComponent();
Initialize();
RealAction(ExProgram, ExCommand);
}
public void Initialize()
{
Readoutput += new delegateReadoutput(LogInfo_Readoutput);
Erroroutput += new delegateErroroutput(LogInfo_Erroroutput);
}
private void RealAction(string StartFileName, string StartFileArg)
{
Process CmdProcess = new Process();
CmdProcess.StartInfo.FileName = StartFileName; // 命令
CmdProcess.StartInfo.Arguments = StartFileArg; // 参数
CmdProcess.StartInfo.CreateNoWindow = true; // 不创建新窗口
CmdProcess.StartInfo.UseShellExecute = false;
CmdProcess.StartInfo.RedirectStandardInput = true; // 重定向输入
CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出
CmdProcess.StartInfo.RedirectStandardError = true; // 重定向错误输出
//CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
CmdProcess.OutputDataReceived += new DataReceivedEventHandler(CmdProcess_OutputDataReceived);
CmdProcess.ErrorDataReceived += new DataReceivedEventHandler(CmdProcess_ErrorDataReceived);
CmdProcess.EnableRaisingEvents = true; // 启用Exited事件
CmdProcess.Exited += new EventHandler(CmdProcess_Exited); // 注册进程结束事件
CmdProcess.Start();
CmdProcess.StandardInput.WriteLine(StartFileArg + "&exit");
CmdProcess.StandardInput.AutoFlush = true;
CmdProcess.BeginOutputReadLine();
CmdProcess.BeginErrorReadLine();
// 如果打开注释,则以同步方式执行命令,此例子中用Exited事件异步执行。
// CmdProcess.WaitForExit();
}
void CmdProcess_Exited(object sender, EventArgs e)
{
MessageBox.Show("进程结束!");
this.Close();
}
void CmdProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && !HasDispose)
{
this.Invoke(Erroroutput, new object[] { e.Data });
}
}
void CmdProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null && !HasDispose)
{
this.Invoke(Readoutput, new object[] { e.Data });
}
}
void LogInfo_Erroroutput(string result)
{
textBox2.AppendText(result + "\r\n");
}
void LogInfo_Readoutput(string result)
{
textBox1.AppendText(result + "\r\n");
}
bool HasDispose = false;
private void LogInfo_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult meDR = MessageBox.Show("是否结束备份!", "提示信息", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (meDR == DialogResult.Yes)
{
HasDispose = true;
BackupClass.KillProcess("cmd");
BackupClass.KillProcess("exp");
this.Dispose();
}
else if (meDR == DialogResult.Yes)
{
HasDispose = true;
e.Cancel = false;
}
else if (meDR == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}void CmdProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null && !HasDispose) { this.Invoke(Erroroutput, new object[] { e.Data }); } } void CmdProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null && !HasDispose) { this.Invoke(Readoutput, new object[] { e.Data }); } }
这两句过不了