473,441 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

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

regards,

--
Alvo von Cossel I of Germany
Nov 17 '05 #1
2 1299
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" <Al************@discussions.microsoft.com> wrote in
message news:5C**********************************@microsof t.com...
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,

--
Alvo von Cossel I of Germany

Nov 17 '05 #2
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:
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" <Al************@discussions.microsoft.com> wrote in
message news:5C**********************************@microsof t.com...
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,

--
Alvo von Cossel I of Germany


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.