Simple app....silly question?

Discussion in 'Microsoft C# .NET' started by David Bonnell, Oct 29, 2008.

  1. 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?
    David Bonnell, Oct 29, 2008
    1. Advertisements

  2. >
    > 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
    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.

    David Bonnell, Oct 29, 2008
    1. Advertisements

  3. >
    > 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.

    David Bonnell, Oct 30, 2008
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Stüben

    silly newbie question - application with more than one form

    Christian Stüben, Aug 5, 2003, in forum: Microsoft C# .NET
    Chris Hornberger
    Aug 5, 2003
  2. Constantine Oxenenko

    Silly Delegation signature question

    Constantine Oxenenko, Aug 29, 2003, in forum: Microsoft C# .NET
    Constantine Oxenenko
    Aug 29, 2003
  3. kalantre

    Silly Windows Forms Question

    kalantre, Feb 25, 2004, in forum: Microsoft C# .NET
    Morten Wennevik
    Feb 25, 2004
  4. Brent

    Silly user feedback question...

    Brent, Nov 2, 2005, in forum: Microsoft C# .NET
    Nov 2, 2005
  5. Chris S

    Service App, Form App, or Console App?

    Chris S, Jun 5, 2006, in forum: Microsoft C# .NET
    Chris S
    Jun 5, 2006