Timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvo von Cossel I

    #1

    Timer

    hi,

    i have 2 forms. when the application runs, 1 of them should run for 300
    milliseconds and then closes. after this, the other form should open. this
    works but the second form closes after that. what happened? can anyone 'fix'
    my code?

    here is my current code:

    private void timer1_Tick(obj ect sender, EventArgs e)
    {
    Form1 f = new Form1();
    f.ShowDialog();
    f.Dispose();
    }

    regards,

    --
    Alvo von Cossel I of Germany
  • Frisky

    #2
    Re: Timer

    The problem you are having is that if you Close() the main form, then the
    application is done. That is because when you started up your application,
    your code said something to the effect of:

    [STAThread]
    static void Main()
    {
    Application.Run (new Form1());
    }

    This is the default code that is generated when you create a new C# WinForm
    application.

    In this instance Form1 is the main form for you application. It is the one
    on which the message loop is centered. If you destroy that window, then the
    application is complete.

    So lets say Form1 contains a timer1 that is set to 200 ms, and you have
    these methods in the form:

    private void Form1_Load(obje ct sender, System.EventArg s e)
    {
    Application.DoE vents();
    timer1.Enabled = true;
    }

    private void timer1_Tick(obj ect sender, System.EventArg s e)
    {
    timer1.Enabled = false;
    Close();
    }

    BTW: Application.DoE vents() should be used with care. I used it here because
    200 ms elapses before my from can paint itself, and I like to see it before
    it goes way. I also wait for the form to be painted before I turn the timer
    on.

    This handled Form1 opening and then closing after 200 ms.

    Now, if we change our Main to look like this:

    [STAThread]
    static void Main()
    {
    Application.Run (new Form1());
    Application.Run (new Form2());
    }

    Then Form2 is launched after Form1 is closed. Now, this has a particular
    implementation, and may not be ultimatley what you were looking for. But,
    maybe by contrast it can help you figure that out.

    For example, if you are trying to display one of those old skool startup
    banners, you might take a different approach. In the case of a startup
    banner, you may want to load a bunch of stuff in the main application while
    the program is loading. You can't do this in the solution I have presented.
    But, instead, you could load Form1, but have it's Visible property set to
    false. On Load, you could spawn a thread (or just show and
    Application.DoE vents()), a Form2, which is your splash screen. Then,
    continuing in your OnLoad of your form, load up all that time consuming
    stuff you need. When you are done loading, bring up your new form and close
    the spash screen.

    Hope this helps...

    --
    Frisky

    Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
    "Alvo von Cossel I" <AlvovonCosselI @discussions.mi crosoft.com> wrote in
    message news:5C8544BE-93CB-4FD2-A428-A6A3561456B6@mi crosoft.com...[color=blue]
    > hi,
    >
    > i have 2 forms. when the application runs, 1 of them should run for 300
    > milliseconds and then closes. after this, the other form should open. this
    > works but the second form closes after that. what happened? can anyone
    > 'fix'
    > my code?
    >
    > here is my current code:
    >
    > private void timer1_Tick(obj ect sender, EventArgs e)
    > {
    > Form1 f = new Form1();
    > f.ShowDialog();
    > f.Dispose();
    > }
    >
    > regards,
    >
    > --
    > Alvo von Cossel I of Germany[/color]


    Comment

    • Alvo von Cossel I

      #3
      Re: Timer

      Thanks for the help, but i changed the code to:

      static void Main()
      {
      Application.Run (new Form2());
      }

      can you still help me?

      thanks anyway

      --
      Alvo von Cossel I of Germany


      "Frisky" wrote:
      [color=blue]
      > The problem you are having is that if you Close() the main form, then the
      > application is done. That is because when you started up your application,
      > your code said something to the effect of:
      >
      > [STAThread]
      > static void Main()
      > {
      > Application.Run (new Form1());
      > }
      >
      > This is the default code that is generated when you create a new C# WinForm
      > application.
      >
      > In this instance Form1 is the main form for you application. It is the one
      > on which the message loop is centered. If you destroy that window, then the
      > application is complete.
      >
      > So lets say Form1 contains a timer1 that is set to 200 ms, and you have
      > these methods in the form:
      >
      > private void Form1_Load(obje ct sender, System.EventArg s e)
      > {
      > Application.DoE vents();
      > timer1.Enabled = true;
      > }
      >
      > private void timer1_Tick(obj ect sender, System.EventArg s e)
      > {
      > timer1.Enabled = false;
      > Close();
      > }
      >
      > BTW: Application.DoE vents() should be used with care. I used it here because
      > 200 ms elapses before my from can paint itself, and I like to see it before
      > it goes way. I also wait for the form to be painted before I turn the timer
      > on.
      >
      > This handled Form1 opening and then closing after 200 ms.
      >
      > Now, if we change our Main to look like this:
      >
      > [STAThread]
      > static void Main()
      > {
      > Application.Run (new Form1());
      > Application.Run (new Form2());
      > }
      >
      > Then Form2 is launched after Form1 is closed. Now, this has a particular
      > implementation, and may not be ultimatley what you were looking for. But,
      > maybe by contrast it can help you figure that out.
      >
      > For example, if you are trying to display one of those old skool startup
      > banners, you might take a different approach. In the case of a startup
      > banner, you may want to load a bunch of stuff in the main application while
      > the program is loading. You can't do this in the solution I have presented.
      > But, instead, you could load Form1, but have it's Visible property set to
      > false. On Load, you could spawn a thread (or just show and
      > Application.DoE vents()), a Form2, which is your splash screen. Then,
      > continuing in your OnLoad of your form, load up all that time consuming
      > stuff you need. When you are done loading, bring up your new form and close
      > the spash screen.
      >
      > Hope this helps...
      >
      > --
      > Frisky
      >
      > Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
      > "Alvo von Cossel I" <AlvovonCosselI @discussions.mi crosoft.com> wrote in
      > message news:5C8544BE-93CB-4FD2-A428-A6A3561456B6@mi crosoft.com...[color=green]
      > > hi,
      > >
      > > i have 2 forms. when the application runs, 1 of them should run for 300
      > > milliseconds and then closes. after this, the other form should open. this
      > > works but the second form closes after that. what happened? can anyone
      > > 'fix'
      > > my code?
      > >
      > > here is my current code:
      > >
      > > private void timer1_Tick(obj ect sender, EventArgs e)
      > > {
      > > Form1 f = new Form1();
      > > f.ShowDialog();
      > > f.Dispose();
      > > }
      > >
      > > regards,
      > >
      > > --
      > > Alvo von Cossel I of Germany[/color]
      >
      >
      >[/color]

      Comment

      Working...