Simple app....silly question?

D

David Bonnell

I have a Windows Forms app that is intended to launch via the SendTo
feature. I want it to perform work (in the background) while updating
a progress bar in the main window.

The problem: I want this to run without any user interaction (except
for launching the app via SendTo).
The form is created via an Application.Run(new myForm()) call, which
then proceeds through the form constructor.

When the constructor returns and the form is displayed, I have no way
of instructing the application to "begin" work. As a workaround, I
temporarily placed a button on the form for the user to initiate work.

I must be missing something. This seems like it would be trivial to
implement. I was considering using a timer as a hack, but surely
there is a better option. Launching a worker thread before exiting
the constructor doesn't strike me as a very good idea either...

Any ideas?
 
D

David Bonnell

I'm not sure I understand the question.  Do you not want the form to  
display at all?  Or do you simply want things to happen while the form is  
displayed without the user having to specifically interact with the form?

I want the form to display (the form should consist of only a progress
bar and possibly a status label).
As work is being done (some file manipulation), I want the progress
bar to update to give the user some feedback that the task is being
completed.
Once the task is complete, I want the application to exit.
If the latter, then I don't see what your objection to starting a worker  
thread in the constructor is -- that should work fine -- but if you don't 
like that, you could instead do something similar in the form's OnLoad()  
or OnShown() method.  

I guess I'm not a fan of doing too much in a constructor. Probably
more a lack of understanding than anything...
OnLoad/Shown seems like a good idea. I'll give it a try and see what
shakes loose.

Thanks,
Dave
 
D

David Bonnell

I suppose that might be a consideration here.  It depends on how you  
implement the worker thread.  But from a more general point of view, as 
long as you're not doing something that will lead to an illegal operation,  
I don't think that it's necessarily a bad thing to put initialization code  
in the constructor.  It has to run _sometime_, and the constructor is in  
fact a place where things are supposed to get initialized.  :)

OnShown worked like a charm. Thanks for the help.

I suspect that in this case it would work fine in the
constructor...but I am my own worst enemy. A month from now I'd
probably add more initialization to the constructor and miss some GUI
updates from the worker thread as a result.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top