调用backgroundWorker1.RunWorkerAsync()函数,用于一个接一个c#windowsform之后的两个函数

时间:2017-07-26 18:49:30

标签: c# backgroundworker windows-forms-designer

我想一个接一个地调用backgroundWorker1.RunWorkerAsync()函数,但是我遇到了这个错误:

  

“此BackgroundWorker当前正忙,无法同时运行多个任务。”

我知道有什么问题。有没有办法做到这一点?这是我想要做的一个样本,实际上有许多字符串和许多功能,其中一个接一个。

如果不可能,我怎么能创建一个windowsForm来在标签中逐个显示我的第一个String []元素,当它完成后再次启动并在同一个标​​签中显示我的第二个字符串[]元素?

public Form1() {
   InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
   looking();
}

public void looking()
{
   a();
   b();
}

public void a()
{
   string[] stringArray = { "Ali", "Hasan", "Morad", "Javad" };
   backgroundWorker1.RunWorkerAsync(stringArray);
}

public void b()
{
   string[] stringArray2 = { "Ali2", "Hasan2", "Morad2", "Javad2", "Mahmood", "Shaparak" };
   backgroundWorker1.RunWorkerAsync(stringArray2);
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   ...
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   ...
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   ...
}

1 个答案:

答案 0 :(得分:0)

使用 private async void button1_Click(object sender, EventArgs e) { await lookingAsync(); } public async Task lookingAsync() { string[] stringArray = { "Ali", "Hasan", "Morad", "Javad" }; foreach (var item in stringArray) { label1.Text = item; await Task.Delay(1000); } string[] stringArray2 = { "Ali2", "Hasan2", "Morad2", "Javad2", "Mahmood", "Shaparak" }; foreach (var item in stringArray2) { label1.Text = item; await Task.Delay(1000); } //if you want it in a loop just uncomment this // await lookingAsync(); } 今天不再被认为是一种好习惯。看看JSFiddle
 在你的情况下,你可以试试这个

根据您的更新,您可以实现此目标

kafkaTopic:testNewTopic