Solved

C# Application.Restart() does not restarting the application...

Posted on 2008-06-16
9
4,883 Views
Last Modified: 2008-08-03
Hi,

I wrote the C# application and i want it to restart is from scratch if some conditions satiesfied. I can see that even if calling to Application.Restart() function, the following activities are continues and i has to exit the execution instance of the application. Is this a expected behavior? I dont think so it should terminate the current instance and start immediately new instance of an applicatiion.

Code:
------
    line#1;
    line#2;
    ..........
   Application.Restart();
   .....Still comming here and doing the task....I have to write Environment.Exit(1) to exit the current instance...
............

Could any one please suggests me what wrong is here,

Thnaks,

0
Comment
Question by:deshaw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
9 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21791422
My advise for doing this is to start a new process and right after end the current.

To do this write something like the attached code.
			System.Diagnostics.Process.Start(Application.ExecutablePath);
			Application.Exit();

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21791439
Just to prevent that any code executes after the exit instruction add a Return right after.
System.Diagnostics.Process.Start(Application.ExecutablePath);
Application.Exit();
return;

Open in new window

0
 
LVL 6

Expert Comment

by:MuhammadAdil
ID: 21791850
Its execute all block in which you are using Application.Restart(). After executing it, application call dispose method. And start your application. In which event you are calling Application.Restart(), there should not any code after this call in that block.

Regards,
Adil Fazal
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:deshaw
ID: 21811527
If there should not be any code after that block then what is the use of Application.Restart()? What if my current function from where i am executing Application.Restart  has been called from some function?

>>System.Diagnostics.Process.Start(Application.ExecutablePath);
>>Application.Exit();

I know this but then there is no point to have Application.Restart function. This means start and restart are the same functions. :-)

Thanks,
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21811626
If you know you want to restart your application, so you just have to isolate that code inside a IF or something and call return; right after the Restart statement.


Something like:

if (IsToRestart)
{
     Application.Restart();
     Return;
}
... nothing more is executed...


The act of restarting an application must be well designed, you must know exactly what will be executed after the method is called.


"What if my current function from where i am executing Application.Restart  has been called from some function?"
Set a return value on that function that executes the Application.Restart so that on the caller function you know that a Restart is being performed.


	public void DoSomeStuff()
	{
		// some code to do whatever you need
 
		if (RestartMyApp()) return;
 
		// some code to do whatever you need
	}
 
	public bool RestartMyApp()
	{
		// some code to do whatever you need
 
		if (IsToRestart)
		{
			Application.Restart();
			return;
		}
 
		// some code to do whatever you need
	}

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 21811635
Sorry mate, the RestartMyApp() function was wrong, here is the correction.

Sorry.
	public void DoSomeStuff()
	{
		// some code to do whatever you need
 
		if (RestartMyApp()) return;
 
		// some code to do whatever you need
	}
 
	public bool RestartMyApp()
	{
		// some code to do whatever you need
 
		if (1 == 1)
		{
			Application.Restart();
			return true;
		}
 
		// some code to do whatever you need
 
		return false;
	}

Open in new window

0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 21811659
Damn... I ended up changing the if condition too but I think you got the idea.
Anyway, here's the code :)

Any problems please let me know.

Thanks
        public void DoSomeStuff()
        {
                // some code to do whatever you need
 
                if (RestartMyApp()) return;
 
                // some code to do whatever you need
        }
 
        public bool RestartMyApp()
        {
                // some code to do whatever you need
 
                if (IsToRestart)
                {
                        Application.Restart();
                        return true;
                }
 
                // some code to do whatever you need
 
                return false;
        }

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to copy file 14 88
global Variable - 2 functions in powershell 1 44
WPF and Powershell button hover change text 5 61
What!!!??? 5 60
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question