奇怪的进度栏行为 [英] Weird Progress bar behavior

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

问题描述

当用户等待冗长的Web服务调用时,我试图显示无限制的进度条。这就是我对BackgroundWorker所做的。 ProgressBarForm是我为进程条创建的无边框对话窗口,没有别的。进度
bar样式设置为Marquee。

I was try to show an indefinite progress bar when the user is waiting for a lengthy web service call. So here is what I did with BackgroundWorker. The ProgressBarForm is a borderless dialog window i created for the process bar and nothing else. The progress bar style is set as Marquee.

        ProgressBarForm bar = new ProgressBarForm();
        BackgroundWorker bwCreate = new BackgroundWorker();
        AutoResetEvent doneEvent = new AutoResetEvent(false);

        try
        {

            Action<object, DoWorkEventArgs> actualCreate = delegate(object sender, DoWorkEventArgs e)
            {
                try
                {  // do something slowly.

                }

                finally
                {
                    doneEvent.Set();
                }
            };

            bwCreate.DoWork += new DoWorkEventHandler(actualCreate);

            bar.Show();
            // bar.ShowDialog();

            bwCreate.RunWorkerAsync();

            doneEvent.WaitOne(100000, true);  //

        }

        finally
        {
            bar.Dispose();

            // bar.Close();

            // bar.Hide();

        }




所以问题是:当我使用ShowDialog()来显示进度条时。进度条本身可以使用选框动画正确渲染。但是,表单不会关闭,进度被阻止。 (我可以使用Alt + F4强制关闭进度
栏并恢复执行但这不是预期的方式)

So the issue is: When I use ShowDialog() to the show the progress bar. The progress bar itself renders properly with marquee animation. However, the form just doesn't close, and the progress was blocked. (I could use Alt+F4 to force close the progress bar and resume the execution but that's not the intended way)

当我使用Show()做同样的事情时,进度条表单可以按预期关闭,但进度条永远不会呈现动画。所以在一段时间后,进度条没有任何结果,它关闭,程序继续。

When I use Show() to do the same thing, the progress bar form could close as intended, but the progress bar never renders the animation. So after some time with nothing happening to the progress bar, it closes and the program continues.

任何人都知道为什么会发生这种情况?更多的背景是该解决方案是XBAP应用程序,但大多数项目都是正常运行的winform项目。

anyone has some idea on why this is happening? A bit more background is that the solution is a XBAP application but majority of the projects are winform projects that run properly.

推荐答案

尽快因为我 提交问题,我想我找到了答案。

As soon as I  submitted the question, i think i found the answer.

似乎需要在ShowDialog()调用之前启动后台工作线程,因为后者可能会阻止程序(在某处读取)。

It seems the backgroundworker thread needs to be started BEFORE the ShowDialog() call as the latter may block the program (read it somewhere).

我还添加了一个RunWorkerComplete事件处理程序,并关闭该事件中的进度条表单。

I also added a RunWorkerComplete event handler and close the progress bar form in that event.

现在似乎工作正常。

对于提出可能的非问题感到抱歉。

Sorry about bringing up a possible non-issue.


这篇关于奇怪的进度栏行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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