Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have an application installed in my system which is developed using windows forms. Every 2 mins the application should exit and start. Can anyone please help me with the sample code
Posted

Try this code:
C#
private Timer timer;
private int count;
private void Form1_Load(object sender, EventArgs e)
{
    count = 0;
    timer = new Timer();
    timer.Interval = 1000;//1 second for tick
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
    count++;
    if (count == 120)//2 minutes to restart application
    {
        count = 0;
        timer.Stop();
        Application.Restart();
    }
}
 
Share this answer
 
Comments
#realJSOP 6-Jan-11 7:24am    
I think this is a better answer than creating a windows service (although I avoid using timers at all costs, and would probably create a thread instead).
Why does it have to restart? Why can't you just reintitialize everything every two minutes (including loading and data files that might be part of the program's initialization?

EDIT ===============

If you don't present ALL of the requirements in your question, you won't get a proper answer right away.

If your app is spawning the apps from a central program, you don't need a timer - all you need to do is respond to the appropriate Process object event that indicates when a spawned process terminates. At that time, you can restart it.
 
Share this answer
 
v3
Comments
Espen Harlinn 6-Jan-11 7:27am    
5+ Agree
pdmkosireddi 6-Jan-11 7:45am    
In this application load event I am starting other applications if they are not running. so every 2 mins this application should make sure all those are runnung. For that purpose what is the best solution...Please suggest..Initialisation ir restart?
jerrykid 6-Jan-11 8:25am    
I don't agree,
If windows application have many forms which are MDI children of the main form, an all of them are in Japanese language, but user select English language, and now how can we reinitialize resources???
#realJSOP 6-Jan-11 8:40am    
@jerrykid - A properly designed application wouldn't have these concerns.
Pete O'Hanlon 6-Jan-11 8:48am    
@jerrykid - if this is an issue, your application can easily hook into the windows messages that indicate that the current culture has changed.
The simplesst way could be to have another application running all the time and have it restart and close the first one every few (two) minutes.

However, if you intend to run an application that needs to restart after a small interval you might want to consider windows services.
 
Share this answer
 
Comments
Venkatesh Mookkan 6-Jan-11 7:06am    
As Abhinav said, I would as suggest the later option only. Good Answer Abhinav
Espen Harlinn 6-Jan-11 7:10am    
Go with the later option - restarting every 2 mins seems like a crappy way to do things ... I know I wouldn't like such a solution in my environment
JF2015 6-Jan-11 7:20am    
Good that you showed the OP how he should approach the problem in a prefered way. Have my 5+
Rajesh Anuhya 6-Jan-11 7:27am    
Good answer
Hi,

Abhinav posted the perfect solution using a service. If you don't want to go that way you could setup a 2 minute timer and then use the Application.Restart method as described here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart.aspx[^]
 
Share this answer
 
Comments
jerrykid 6-Jan-11 8:26am    
good, 5+
Manfred Rudolf Bihy 6-Jan-11 14:19pm    
Good call! 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900