다음 명령을 시작하기 전에 각 명령이 완료되기를 기다리는 명령을 세 개 있습니다.다음 명령을 시작하기 전에 각 명령이 완료되기를 기다리는 여러 명령을 어떻게 실행할 수 있습니까?
첫 번째 작업을 완료 한 후 내 구현을 기반으로하지만 두 번째 작업은 시작되지만 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]). – DonBoitnott
도움이됩니다. https://stackoverflow.com/questions/1728099/visual-c-sharp-gui-stops-responding-when-process-waitforexit-is-used – DonBoitnott