저는 C#을 처음 접했고 배경 작업자를 이해하려고합니다.백그라운드 작업자 C#에서 프로세스로 실행되는 동안 cmd.exe 창을 닫는 방법?
현재이 코드를 실행하면 StopButton을 클릭 한 후 명령 프롬프트에서 출력을 읽거나 리디렉션하는 것을 중지하고 "취소됨"메시지를 남겨 둡니다. 그러나 나중에 아무 작업도 수행하지 않습니다. 현재이 모든 잘못된 구현할 수 있지만 e.Cancel CancellationPending = true로 CancelAsync(), 호출하는 중지 단추를 클릭하여 설정할 수 있습니다. 누구든지 내가 어떻게해야하는지에 대한 아이디어가 있습니까?
감사합니다. 도움을 주셔서 감사합니다.
public Form2()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
}
private void StopButton_Click(object sender, EventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync();
bw.Dispose();
Invoke(new ToDoDelegate(() => this.textBox2.Text += Environment.NewLine + "Cancelled " + Environment.NewLine));
}
}
private void Submit_Click(object sender, EventArgs e)
{
if(bw.IsBusy != true)
{
bw.RunWorkerAsync();
Invoke(new ToDoDelegate(() => this.textBox2.Text += "Starting Download " + Environment.NewLine));
}
}
public bool DoSVNCheckout(String KeyUrl, DoWorkEventArgs e)
{
SVNProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C plink download files using svn"
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false,
}
};
SVNProcess.Start();
while(!SVNProcess.StandardOutput.EndOfStream & bw.CancellationPending == false)
{
string output = SVNProcess.StandardOutput.ReadLine();
Invoke(new ToDoDelegate(() => this.textBox2.Text += output));
}
while (!SVNProcess.StandardError.EndOfStream & bw.CancellationPending == false)
{
string Erroutput = SVNProcess.StandardError.ReadLine();
Invoke(new ToDoDelegate(() => this.textBox2.Text += Erroutput));
}
if(SVNProcess.StandardError.EndOfStream & bw.CancellationPending == false)
{
string Erroutput = SVNProcess.StandardError.ReadLine();
Invoke(new ToDoDelegate(() => this.textBox2.Text += Erroutput));
}
//if I manually close the cmd.exe window by clicking X
//in the top right corner the program runs correctly
//at these lines of code right here
if(bw.CancellationPending == true)
{
e.Cancel = true;
return true;
}
return false;
}
private delegate void ToDoDelegate();
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if(bw.CancellationPending == true)
{
e.Cancel = true;
return;
}
e.Cancel = DoSVNCheckout(URL, e);
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.textBox2.Text = "Canceled!";
}
else{
this.textBox2.Text = "Done!";
}
}
이 비동기 어떻게 표시되지 않습니다 :
완성도를 들어, 다음은 위의
Form1
클래스와 함께가는 디자이너가 생성 한 코드입니다. 이것은 단지 다중 쓰레딩입니다.[CancellationTokenSource] (https://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource (v = vs.110) .aspx)를 살펴보십시오. –"CancelAsync()"명령을 사용하면 안된다는 말씀입니까? 이 명령은 현재 작업을 중지하는 것처럼 보이지만 나중에는 아무 작업도 수행하지 않습니다. –
CancellationTokenSource 및 CancellationToken을 사용하는 것이 좋습니다. 그것이 정확하게 설계된 것입니다. –