Solved

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

Posted on 2008-06-16
9
4,802 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
  • 5
9 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:deshaw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now