如何使用一个BackgroundWorker? [英] how to use a backgroundworker?
问题描述
我kwnow它有3种方法
那么在我的节目,我有发送消息就晚了很多的方法
程序不某个时候发送消息时,我preSS的按钮,我不知道为什么
它晚5 secunds和程序frezed另一个次,然后我想提出一个BackgroundWorker的邮件被发送,我有code代表发送消息
在按钮现在在哪里,我去把这个code?
在按钮我要去把这个code ...
i kwnow it has 3 methods well in my program i have a method for to send a message it late a lot of the program doesn't send the message sometime when i press the button i dont know why another times it late as 5 secunds and the program is frezed then i want to put a backgroundworker as the message is being sending i had the code for send the message in a button now where do i go to put this code? in the button i am going to put this code...
backgroundWorker1.RunWorkerAsync();
是不是?
在
isn't it? and in
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{}
我要去把我的code按钮的不是吗?
而在此之前
i am going to put my code of the button isn't it? and this before
carga.progressBar1.Minimum = 0;
carga.progressBar1.Maximum = 100;
carga是我的另一种形式,其中进度是
carga is my another form where the progressbar is
我如何使用一个BackgroundWorker:我很失落的这个
how do i use a backgroundworker :s i am lost in this
推荐答案
您只能从 ProgressChanged
或 RunWorkerCompleted $ C更新进度条$ C>事件处理程序,因为这些都与UI线程同步。
You can update progress bar only from ProgressChanged
or RunWorkerCompleted
event handlers as these are synchronized with the UI thread.
的基本思想是。 Thread.sleep代码
只是模拟一些工作在这里。与你的真实电话的路由,将其更换。
The basic idea is. Thread.Sleep
just simulates some work here. Replace it with your real routing call.
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.WorkerReportsProgress = true;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
backgroundWorker1.ReportProgress(i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
这篇关于如何使用一个BackgroundWorker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!