Windows应用程序正在加载数据负载. [英] Windows application loading loads of data.
问题描述
嘿,我只是在寻找一些建议,以哪种方式来处理我的应用程序中需要很长时间的特定任务.
好的,任务是上传大量数据,一次需要花费几分钟.我想做的是让用户在执行其他任务时继续执行单独的任务,我曾考虑过要放置背景工人和进度条,但是网上没有明确的简洁示例.无论如何,我对任何建议都持开放态度,对我们的帮助也非常感激.
Hey I''m just looking for a few suggestions as which way to handle a specific task in my application that takes a very long time.
Ok the task is to upload a large chunk of data which takes a couple of minutes at a time. What I want to do is let the user continue doing seperate tasks while this other task is taking place, I have thought about putting a backgroung worker and a progress bar but there are no clear concise examples on the net. Anyway im open to any suggestions and any help is very much apreciated.
推荐答案
后台工作人员非常非常容易做:
创建一个BackgroundWorker实例,并将其连接到几个事件:
A background worker is very, very easy to do:
Create a BackgroundWorker instance, and hook it to a couple of events:
private BackgroundWorker worker = new BackgroundWorker();
...
butDoMyTask.Enabled = false;
ShowProgress.Visible = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerReportsProgress = true;
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
然后在事件中,您只需要显示进度,完成实际工作并在完成时进行标记即可:
Then in the events, you just have to show progress, do the actual work, and mark when it completes:
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
butDoMyTask.Enabled = true;
ShowProgress.Visible = false;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ShowProgress.Value = e.ProgressPercentage;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i += 10)
{
Thread.Sleep(1000);
worker.ReportProgress(i);
}
}
您是对的!使用后台工作者有一个简单的步骤:
1.启动进度栏
2. BackgroundWorker1.RunWorkerAsync()[您想放置的位置]
3.在BackgroundWorker1_DoWork事件上,执行数据加载操作.
4.在BackgroundWorker1_RunWorkerCompleted事件上,停止进度栏.
注意:请勿交叉穿线.会报错.
You were right! Use background worker there is simple step:
1. Start progress bar
2. BackgroundWorker1.RunWorkerAsync() [where evere you want to put]
3. On BackgroundWorker1_DoWork Event, do the data loading thing.
4. On BackgroundWorker1_RunWorkerCompleted event , stop Progress bar.
Caution: Don’t go cross thread. It will give error.
这篇关于Windows应用程序正在加载数据负载.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!