简体   繁体   English

后台工作程序中的进度栏​​未加载

[英]Progress Bar in Background Worker not loading

I've run into an issue that I can't seem to figure out the solution to, I've read several answers and have gotten as far as knowing that I need a BackgroundWorker Thread to make this solution work, but I'm still running into a bit of an issue. 我遇到了一个似乎无法找出解决方案的问题,我已经阅读了几个答案,并且知道我需要一个BackgroundWorker线程才能使该解决方案正常工作,但是我仍然遇到了一个问题。

I have a second form that is a small size and set to center on screen with a ProgressBar Style set to Marquee, there is nothing else on this second form as it is meant to emulate a loading bar. 我有一个较小的第二个窗体,它的进度条样式设置为Marquee,设置为在屏幕上居中,第二个窗体上没有其他内容,因为它可以模拟加载栏。

In my data intensive section of the code, on the main form, where it grabs and parses data from a database I have it written like this: 在我的代码的数据密集型部分的主窗体上,它从数据库中获取和解析数据,我将其编写为:

 GetData()
 {
      bwLoading.RunWorkerAsync();
      //Runs all the processing
      bwLoading.CancelAsync();
 }

The background worker is defined as such below, also on the main form. 后台工作人员在主窗体上也定义如下。

 private void bwLoading_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        frmLoading lb = new frmLoading();
        bool running = true;
        while (running)
        {
            if ((worker.CancellationPending == true))
            {
                lb.Close();
                running = false;
            }
            else
            {
                lb.Show();
            }
        }

    }

The problem I have run into is that with this method it shows the Loading form that contains the progress bar does show up when the data starts getting gathered and process and does disappear when the data load is complete, but the window is empty like it's trying to load, but it doesn't. 我遇到的问题是,使用此方法时,它会显示包含进度条的“正在加载”表单在数据开始收集和处理时会显示,并且在数据加载完成后会消失,但是窗口却像尝试中的那样是空的加载,但没有加载。 I can't seem to figure out why this isn't working. 我似乎无法弄清楚为什么这行不通。

Isn't the background worker creating a separate thread for the Loading form to use? 后台工作者不是为“加载”表单创建单独的线程以供使用吗? Or is there something else that I'm missing from my reading? 还是我的阅读中缺少其他东西?

You have to actually report the progress of your background operation using ReportProgress() . 你必须用实际报告您的后台操作的进度ReportProgress() This requires you knowing how much percent of your work you have completed. 这需要您知道已完成多少工作。

If you do not report progress, the progress bar will always remain at 0% until your background operation is completed, and disappear afterwards - which is what you are observing. 如果您不报告进度,则进度条将始终保持为0%,直到完成后台操作为止,此后消失(这就是您正在观察的内容)。

Edit: 编辑:

In light of your comment - you are using a marquee style progress bar - I think the problem is here: 根据您的评论-您正在使用选取框样式的进度条-我认为问题出在这里:

 GetData()
 {
      bwLoading.RunWorkerAsync();
      //Runs all the processing
      bwLoading.CancelAsync();
 }

I suspect you execute the GetData() method from the UI thread - that means the UI thread is blocked and the progress bar cannot be updated. 我怀疑您是从UI线程执行GetData()方法的-这意味着UI线程被阻止并且进度条无法更新。 Move all your processing to the bwLoading_DoWork method so it is executed on a separate thread and not the UI thread - then your progress bar should get updated. 将所有处理移至bwLoading_DoWork方法,使其在单独的线程而不是UI线程上执行-然后,进度条应得到更新。 Also remove the bwLoading.CancelAsync() call - this would be used only for cancellation. 还要删除bwLoading.CancelAsync()调用-这仅用于取消。

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

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