Click here to Skip to main content
16,016,332 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:

C#
DialogResult dr = EnvelopeResults.ShowDialog();


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):

C#
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);
 }


Thanks!

What I have tried:

MSDN, Other posts on this site.
Posted
Updated 14-May-16 14:54pm
Comments
Sergey Alexandrovich Kryukov 14-May-16 20:23pm    
Do you mean that you start the worker from the handler of the event Form.Shown? What's the problem with that?
(It's not clear, because showing of just the method does not give the information on its uses as a handler, unless you show you += operator use.)
You may have problem is the worker's method itself, but you did not show it.
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900