增加一个进度条 [英] adding a progress bar

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

问题描述

我必须使用
这种形式有一个按钮的Windows窗体应用程序一个类(它的名字是解析器),当我点击Windows窗体上应用程序按钮,它调用解析器类方法之一。



这方法只是阅读的文本文件的行行后,写每一行分隔文件...



我想一个进度条,以显示进度添加到窗体(这是一个非常大的文件)



任何想法如何做到这一点?我在解析2级属性之一行的文件的数量和第二多少行已选中。



这是我的 button2_Click 函数

 私人无效button2_Click(对象发件人,EventArgs五)
{
如果(this.textBox1 = NULL&安培;!this.textBox2 = NULL)
{
inst.init(this.textBox1.Text,this.textBox2.Text);
//this.progressBar1.Show();

inst.ParseTheFile();
System.Windows.Forms.MessageBox.Show(解析成功完成);
}
}


解决方案

您可以这样做:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
ThreadPool.QueueUserWorkItem( (OBJ)=>
{
VAR线= File.ReadLines(@D:\test.txt);
变种数= lines.Count();

。对于(INT I = 0; I<计数;我++)
{
//一些解析工作
调用(新动作(()=>
{
progressBar1.Value =(I * 100)/计数;
}));
}
});
}

在上面的例子中,它仅仅是为了不阻止创建后台线程UI线程,直到调用方法被调用。
中的调用方法是必要的,以便用控制当前线程是不是操纵的所有者。这需要一个委托,并运行在拥有控制的线程上这个委托。



您甚至可以去当迄今为止,作为使的foreach 环路平行的,如果它是耗时的任务解析行的时候。举个例子:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
ThreadPool.QueueUserWorkItem( (OBJ)=>
{
VAR线= File.ReadLines(@D:\test.txt);
变种数= lines.Count();

的Parallel.For(0,算上,我= GT;
{
//一些解析工作
调用(新动作(()=>
{
progressBar1.Value =(I * 100)/计数;
}));
});
});
}


I have an a windows form application that use one class (its name is Parser) this form has a button and when i click on the windows form application button it call one of the parser class method .

this method simply read text file line after line and write each line to separate file...

i would like to add a progress bar to the form in order to show the progress( it is a very large file )

any idea how to do that? I have in the Parse class 2 property one for the number of line in the file and the second how much lines already checked.

here is my button2_Click function

private void button2_Click(object sender, EventArgs e)
{
      if (this.textBox1 != null & this.textBox2 != null)
      {
           inst.init(this.textBox1.Text, this.textBox2.Text);
           //this.progressBar1.Show();

           inst.ParseTheFile();
           System.Windows.Forms.MessageBox.Show("Parsing finish successfully"); 
       }
}

解决方案

You could do:

private void button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem((obj) =>
    {
        var lines = File.ReadLines(@"D:\test.txt");
        var count = lines.Count();

        for(int i = 0; i < count; i++)
        {
            // some parse work
            Invoke(new Action(() =>
            {
                progressBar1.Value = (i * 100) / count;
            }));
        }
    });
}

In the example above, it simply creates a background thread in order not to block the UI thread, until the Invoke method is called. The Invoke method is necessary, in order to manipulate with a Control that the current thread isn't the owner of. It takes a delegate, and runs this delegate on the thread that owns the Control.

You could even go as far, as making the foreach loop parallel, if it's a time consuming task to parse the lines. An example:

private void button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem((obj) =>
    {
        var lines = File.ReadLines(@"D:\test.txt");
        var count = lines.Count();

        Parallel.For(0, count, i =>
        {
            // some parse work
            Invoke(new Action(() =>
            {
                progressBar1.Value = (i * 100) / count;
            }));
        });
    });
}

这篇关于增加一个进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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