Simple app....silly question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Bonnell

    Simple app....silly question?

    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?

  • Peter Duniho

    #2
    Re: Simple app....silly question?

    On Wed, 29 Oct 2008 11:01:25 -0700, David Bonnell <dbonnell@gmail .com>
    wrote:
    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.
    >
    [...]
    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?
    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?

    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. You'll definitely want to use a worker thread of
    _some_ form, such as BackgroundWorke r, to ensure that the UI remains
    responsive.

    Pete

    Comment

    • David Bonnell

      #3
      Re: Simple app....silly question?

      >
      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

      Comment

      • Peter Duniho

        #4
        Re: Simple app....silly question?

        On Wed, 29 Oct 2008 11:40:52 -0700, David Bonnell <dbonnell@gmail .com>
        wrote:
        [...]
        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.
        The only argument I can think of against initializing the worker thread in
        the constructor is that if the worker thread should get to a point where
        it wants to update the UI before the form has actually gotten a chance to
        be fully initialized, then the call to Invoke() (whether explicit in your
        code or implicit via a BackgroundWorke r or similar) could fail in the
        worker thread.

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

        But, OnShown() should work fine...it's pretty much the latest method/event
        that gets called/raised as the form is being created and run, other than
        OnPaint() (which would be a silly place to put initialization :) ).

        Pete

        Comment

        • David Bonnell

          #5
          Re: Simple app....silly question?

          >
          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...b ut 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

          Comment

          Working...