如何在显示进度表单时自动执行runworkerasync [英] How to automatically execute runworkerasync when a progress form is dispalyed

查看:77
本文介绍了如何在显示进度表单时自动执行runworkerasync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,



对于我的应用程序,我需要一个进度条表单,用于长时间运行的数据包络工作/大型OpenXML Excel文件写入,所以我有创建了一个名为RunEnvelope_Form的弹出表单。它很简单,包含进度条,状态框,取消按钮和关闭按钮。我正在利用一个后台工作人员来完成工作正常的数据包络/ Excel文件编写工作。



为了让BackgroundWorker.RunWorkerAsync自动执行,我已将它添加到RunEnvelope_Form的Shown事件的末尾( SEEMED 好像一个好主意)。 BackgroundWorker成功执行,但我遇到了一些问题:



[1]进度窗口无法移动

[2]最重要的是在执行RunWorkerCompleted事件之后,取消按钮无响应。



想一想,因为我怀疑主表单还没有进入消息循环吗?



所以我的问题是,在显示表单时自动启动RunWorkerAsync的最佳方法是什么?我真的很想避免一个启动按钮,这似乎是我遇到的MSDN示例中的标准。



这是我显示我的表单的方式主应用程序窗口:



All,

For my application, I need to have a progress bar form for a long running data enveloping job/big OpenXML Excel file write, so I have created a "pop-up" form called "RunEnvelope_Form". It is simple and contains a progress bar, status box, cancel button and close button. I am utilizing a Background worker to do the heaving data enveloping/Excel file write work which is working fine.

In order to have the BackgroundWorker.RunWorkerAsync execute automatically I have added it to the end of the Shown event of the RunEnvelope_Form (which SEEMED like a good idea). The BackgroundWorker executes successfully, but I have a few problems:

[1] The progress window cannot be moved
[2] Most importantly the Cancel button is unresponsive until after the RunWorkerCompleted event is executed.

Thinking about it, makes sense since I suspect the main form has not entered the message loop yet?

So my question is, what is the best way to start RunWorkerAsync automatically when a form is displayed? I really really want to avoid a start button which seems to be the norm in the examples on MSDN I have come across.

This is how I display the form from my main application window:

DialogResult dr = EnvelopeResults.ShowDialog();





这是RunEnvelope_Form表单中显示的事件(注意所有的工作线程机制都是在表单的构造函数中设置的):





And this is the Shown event in the RunEnvelope_Form form (note all of the Worker thread machinery is setup in the constructor for the form):

private void RunEnvelope_Form_Shown(object sender, EventArgs e)
 {
     // GUI setup

     CanceAsyncButton_Click.Enabled = true;
     CloseFormOKButton_Click.Enabled = false;
     Status_Label.Text = "";
     EnvelopeProgress_Bar.Value = 0;
     EnvelopeProgress_Bar.Style = ProgressBarStyle.Continuous;
     EnvelopeProgress_Bar.ForeColor = Color.Green;

     // setup a tuple to handle input data to worker thread (I like Tuples)

     Tuple<AnalysisSeries, Dictionary<int, bool>, string> aysncData =
         new Tuple<AnalysisSeries, Dictionary<int, bool>, string>
         (_Aseries, _CasestoRender, _OutputXLS);

     // now can start the envelope process

     EnvelopeResults_Worker.RunWorkerAsync(aysncData);
 }





谢谢!



我尝试了什么:



MSDN,本网站上的其他帖子。



Thanks!

What I have tried:

MSDN, Other posts on this site.

推荐答案

我认为ShowDialog()可能是问题的一部分 - 它建议一个模态对话框 - Show()可能更合适。这里有一个很好的讨论 c# - 是否可以使用ShowDialog没有阻止所有表单? - 堆栈溢出 [ ^ ]可能有帮助
I think the ShowDialog() might be part of the issue - it suggests a modal dialog - Show() might be more appropriate. There's a good discussion here c# - Is it possible to use ShowDialog without blocking all forms? - Stack Overflow[^] that may help


这篇关于如何在显示进度表单时自动执行runworkerasync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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