背景工人&进度条问题 [英] background worker & progress bar question

查看:124
本文介绍了背景工人&进度条问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的申请:

表格1 =>主要形式。当它加载时,它必须从数据库更新一些信息并从磁盘读取一些文件

表格2 =>二级表格



我想显示表格2上有一个进度条,而form1更新其信息并显示他的对话框然后隐藏form2



我将此代码放在Form2类中: BackgroundWorker和ProgressBar演示 [ ^ ]

我输入Form1 LOAD => Form2 f2 = new Form2(); f2.Show()



但我在进度条中没有任何进展...



I have an application like this:
Form 1 => main form. When it loads it must update some information from database and read some files from disk
Form 2=> secondary form

I want to display Form 2 with a progress bar on it while the form1 updates its information and shows up his dialog and then hide form2

I put this code in Form2 class : BackgroundWorker and ProgressBar demo[^]
and i put in Form1 LOAD => Form2 f2=new Form2(); f2.Show()

But i don''t get any progress in the progress bar...

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           // Your background task goes here
           for (int i = 0; i <= 100; i++)
           {
               // Report progress to 'UI' thread
               backgroundWorker1.ReportProgress(i);
               // Simulate long task
               System.Threading.Thread.Sleep(150);
           }
       }







private void Form1_Load(object sender, EventArgs e)
       {


           this.CenterToScreen();
           users = usrsrv.ListaUtilizatori();//next 6 lines communicate with database and adds information in 6 lists
           articles = usrsrv.ListaArticole();
           tests = usrsrv.ListaTeste();
           userarticles = usrsrv.ListaArticoleUtilizator();
           usertests = usrsrv.ListaTesteUtilizator();
           fb = usrsrv.ListaFeedback();
           existentarticles = usrsrv.IncarcareNumeFisiereExistente();//reads from file

           int i = 0;
           foreach (utilizator util in users)
           {
               ut.Insert(i,util.username);
               i++;
           }
           this.ActiveControl = textBox1;

       }

推荐答案

在Form Load事件中,请输入以下代码...



backgroundWorker1.RunWorkerAsync(); - 应将backgroundWorker1控件添加到Form1

Form2 form2 = new Form2();

form2.Show();

- progressBar1是Form2中的一个控件



在BackGroundWorker的DoWork事件中...



private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)

{

- 编写代码以从磁盘读取文件...

}



在BackGroundWorker的RunWorkerCompleted事件中

{

- 写入代码应该在阅读文件后完成并且

form2.Close();

}
In your Form Load event, wite the following code...

backgroundWorker1.RunWorkerAsync(); -- backgroundWorker1 control should be added to Form1
Form2 form2 = new Form2();
form2.Show();
-- progressBar1 is a control in Form2

In the DoWork event of BackGroundWorker...

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
--write your code to read files from disk...
}

In the RunWorkerCompleted Event of BackGroundWorker
{
--write code which should be done after reading files and
form2.Close();
}


这篇关于背景工人&amp;进度条问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆