如何在Windows应用程序中处理进度条连续样式 [英] How to work the progress bar continuous style in windows application

查看:80
本文介绍了如何在Windows应用程序中处理进度条连续样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据集包含7万条记录。如果我们想将数据加载到datagridview.it中可以进行一些处理。在那段时间里,我会不断提高进度条,以便在将数据完成后再添加到datagridview。



但这里是我写的代码,它是否正确任何其他过程都可以做到。



  private   void  btnSubmit_Click( object  sender,EventArgs e)
{

string connstring = 数据源= IICSS75;用户ID = pmisdbadmin;密码= pmisdbadmin;初始目录= PMA23052013;

SqlConnection con = new SqlConnection(connstring);

SqlDataAdapter da = new SqlDataAdapter( 从ATTENDANCE中选择* ,con);

DataSet ds = new DataSet();
da.Fill(ds);



dataGridView1.Visible = true ;


progressBar1.Visible = true ;
progressBar1.Show();

progressBar1.Minimum = 1 ;
progressBar1.Value = 1 ;
progressBar1.Step = 1 ;

progressBar1.Maximum = ds.Tables [ 0 ]。Rows.Count;

for int i = 1 ; i < ds.Tables [ 0 ]。Rows.Count; i ++ )
{
progressBar1.PerformStep();

x = 1 ;


}
if (x == 1
{
dataGridView1.DataSource = ds;
}

解决方案

你不能这样做:Fill方法是一个阻塞调用,这意味着它没有直到它完成为止。因此,它会阻止您对进度控制的更新发生。



最好的解决方案是将慢速操作移到另一个线程,这样就可以了主(UI)线程在操作进行时继续更新显示。看一下 BackgroundWorker类 [ ^ ] - 它就是为此而设计的,链接包括一个例子。


问题是你正在UI线程上做所有的工作。这意味着您更新了进度条应显示的值,但您永远不会让它有机会使用新值重绘自身。这也意味着当数据填充到网格中时,您的应用程序似乎会挂起。



要解决此问题,请将该功能移至其他方法,然后在新线程上执行该方法。因为新方法将在后台线程上运行,所以它可能不与GUI交互。因此,您需要创建另一个新方法来报告进度。需要在GUI线程上调用此方法。



一些链接可帮助您入门:

http://www.csharpque.com/2012/04/progressbar-example-cracked-easy.html [ ^ ]

http://www.csharp-examples.net/asynchronous-method-progress/ [ ^ ]

http://www.albahari.com/threading/part3.aspx [ ^ ]

C#5 A中的进度报告同步 [ ^ ]

Dataset contains 7 lakhs records. if we want to load the data into datagridview.it can take the some processing. In that time i will raise the progress bar with continuously to after conpletion of the data to datagridview.

But here is i wrote the code, it is correct or not and any other process can do it.

private void btnSubmit_Click(object sender, EventArgs e)
        {

           string connstring = "Data Source=IICSS75;User ID=pmisdbadmin;Password=pmisdbadmin;Initial Catalog= PMA23052013";

            SqlConnection con = new SqlConnection(connstring);

            SqlDataAdapter da = new SqlDataAdapter("Select * from ATTENDANCE", con);

            DataSet ds = new DataSet();
            da.Fill(ds);

           

            dataGridView1.Visible = true;
           

            progressBar1.Visible = true;
            progressBar1.Show();

            progressBar1.Minimum = 1;
            progressBar1.Value = 1;
            progressBar1.Step = 1;
           
            progressBar1.Maximum = ds.Tables[0].Rows.Count;

            for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
            {
                progressBar1.PerformStep();

                  x = 1;
                

            }
            if (x == 1)
            {
                dataGridView1.DataSource = ds;
            }

解决方案

You can't do it like that: The Fill method is a blocking call, which means it doesn't return until it is completed. As a result, it prevents you updates to the progress control from happening.

The best solution to this is to move the slow operation into a different thread, which allows your main (UI) thread to continue updating the display while the operation is going on. Have a look at the BackgroundWorker class[^] - it's designed for just this, and the link includes an example.


The problem is that you're doing all the work on the UI thread. This means that you update the value that the progress bar should display, but you never give it a chance to redraw itself with the new value. It also means that your application will appear to hang while the data is being populated into the grid.

To overcome this problem, move that functionality out to a different method, then execute the method on a new thread. Because the new method will be running on a background thread, it may not interact with the GUI. Therefore, you will need to create another new method to report the progress. This method will need to be invoked on the GUI thread.

Some links to help you get started:
http://www.csharpque.com/2012/04/progressbar-example-cracked-easy.html[^]
http://www.csharp-examples.net/asynchronous-method-progress/[^]
http://www.albahari.com/threading/part3.aspx[^]
Progress Reporting in C# 5 Async[^]


这篇关于如何在Windows应用程序中处理进度条连续样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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