Timer

G

Guest

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(object sender, EventArgs e)
{
Form1 f = new Form1();
f.ShowDialog();
f.Dispose();
}

regards,
 
F

Frisky

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(object sender, System.EventArgs e)
{
Application.DoEvents();
timer1.Enabled = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled = false;
Close();
}

BTW: Application.DoEvents() 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.DoEvents()), 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...
 
G

Guest

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 said:
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(object sender, System.EventArgs e)
{
Application.DoEvents();
timer1.Enabled = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
timer1.Enabled = false;
Close();
}

BTW: Application.DoEvents() 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.DoEvents()), 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 said:
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(object sender, EventArgs e)
{
Form1 f = new Form1();
f.ShowDialog();
f.Dispose();
}

regards,
 

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