【发布时间】:2018-04-03 10:50:37
【问题描述】:
我要执行三个命令,等待每个命令完成后再开始下一个命令。
根据我完成第一个后的实现,第二个将启动,但 backgroundWorker1_RunWorkerCompleted 根本不会引发。
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 cmd_commands
{
public partial class Form1 : Form
{
string[] commands = new string[] {
@"test",
@"test1",
@"test2" };
int command = 0;
public Form1()
{
InitializeComponent();
}
public void runCmd(string command)
{
ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe");
cmdsi.Arguments = command;
Process cmd = Process.Start(cmdsi);
cmd.WaitForExit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
runCmd(commands[command]);
backgroundWorker1.ReportProgress(0, command);
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = "Working on command number: " + e.UserState.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
command++;
runCmd(commands[command]);
}
}
}
【问题讨论】:
-
我的回答有帮助吗?
-
除非有更多,否则它最终会崩溃,因为你永远不会检查这个:
runCmd(commands[command])。 -
这应该会有所帮助:*.com/questions/1728099/…
标签: c# .net winforms events backgroundworker