无限循环中的doevent() [英] doevent() in the infinite loop
本文介绍了无限循环中的doevent()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了doevent()是在此无限循环中以形式运行另一个控件的另一种方式.
我想和backgroundworker一起做,但是失败了!!
Except doevent() is another way that, in this infinite loop run another control in form.
I wanted to do it With backgroundworker, but I failed.!
private void button1_Click(object sender, EventArgs e)
{
while (true)
{
}
}
推荐答案
如果我已正确理解要执行的操作,则需要将while
循环及其包含的任何代码替换为BackgroundWorker
.
您声明自己尝试了BackgroundWorker并失败了,但是如果不查看while循环内的代码以及尝试的BackgroundWorker的外观,很难给出更多建议.
If I have understood what you want to do correctly, you need to replace yourwhile
loop and whatever code it contains by aBackgroundWorker
.
You state that you tried a BackgroundWorker and failed but it is very difficult to give more advice without seeing the code inside the while loop and what the BackgroundWorker you tried looks like.
合理的示例我的其他答案中显示了如何使用BackgroundWorker
:
逐步更改以显示控件 [ ^ ].
—SA
A reasonable sample of how to work withBackgroundWorker
is shown in my other answer:
Changes step by step to show a control[^].
—SA
您好,
您的问题尚不清楚,因此很难给出明确的答案.
如果有帮助,这是如何使用后台工作者的示例:
Hello,
Your question is a bit unclear, so it is difficult to give an definite answer.
If it helps this is an example of how you could use a background worker:
BackgroundWorker backgroundWorker;
public Form1()
{
InitializeComponent();
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.WorkerSupportsCancellation = true;
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
// do something...
Console.WriteLine(DateTime.Now.ToLongTimeString());
Application.DoEvents();
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
}
显然,还有其他方法可以做到这一点.
希望对您有所帮助,
瓦莱里.
Obviously there are other ways to do it.
Hope it helps,
Valery.
这篇关于无限循环中的doevent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文