简体   繁体   English

使用后台工作程序执行sql命令时的进度条

[英]Progress bar while sql command execute using background worker

I'm trying to create a progress bar wich shown up when the calculate button clicked. 我试图创建一个进度条,单击“计算”按钮时将显示该进度条。 And runs until the execute finishes the process. 并运行直到执行完成过程。 I'm using background worker for the operation. 我正在使用后台工作者进行操作。 And the progress bar is just a simple marquee so if the process ends i want to torn false the visible property. 而且进度条只是一个简单的选取框,因此如果进程结束,我想撕裂false可见属性。

here is the initialize 这是初始化

backgroundworker1 = new System.ComponentModel.BackgroundWorker();
        backgroundworker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);

so first when the calculate button clicked: 所以首先当单击计算按钮时:

    private void buttonCalculate_Click(object sender, EventArgs e)
    {
         //execute the background worker 
        this.backgroundworker1.RunWorkerAsync();
        while (this.backgroundworker1.IsBusy)
        {
            progressBar1.Visible=true;
            Application.DoEvents();
        }
    }

Here is the backgroundworker1_DoWork event 这是backgroundworker1_DoWork事件

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            connection = new MySqlConnection(prop.connectionString);
            string query = "Call telephelyi_2013()";

                if (this.OpenConnection() == true)
                {

                    MySqlCommand cmd = new MySqlCommand();
                    cmd = connection.CreateCommand();
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }

        }

and finaly when the operation comleeted it shuld be do this 最后,当操作完成时,应该这样做

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.Visible = false;
        MessageBox.Show("Számítás befejzve!");
    }

But after all when i run the program and click on the calculate button the progress bar shown up and just moving never ends. 但是毕竟,当我运行该程序并单击“计算”按钮时,进度条就会显示出来,并且移动永远不会结束。 And i also see that the computer is calculating for a while and after stops but i never get that end message! 而且我还看到计算机正在计算一段时间,然后停止,但是我从未收到结束消息!

whats wrong? 怎么了?

You need to just start the background worker and then let it go, rather than holding up the main thread by waiting on the worker. 您只需要启动后台工作程序然后放开它,而不是通过等待该工作程序来占用主线程。 Using DoEvents is just a hack that is going to cause more problems than it'll solve unless you're very intimately familiar with what it does, how it works, and when you should use it. 使用DoEvents只是一个hack,它将引起更多的问题,甚至无法解决,除非您非常熟悉它的功能,工作原理以及何时使用它。

Just have your button click do this: 只需单击您的按钮即可:

this.backgroundworker1.RunWorkerAsync();
progressBar1.Visible=true;

And voila. 和瞧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM