0

I am using compact framework 3.5 to build a windows mobile application.I need to restart the application after saving the application settings. I tried the below one,from How do I restart my C# WinForm Application?

   string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
Process.Start(path, "");

I am not getting any error,but my application is not restarting.I am checking in my simulator.Do restart working in mobile simulator.

Need solution to solve this problem.

Thanks

Community
  • 1
  • 1
Royal
  • 752
  • 2
  • 14
  • 29

2 Answers2

3

Try using this code to access the path:

string path;
path = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

But to restart the application you can use RunAppAtTime method:

var time = DateTime.Now.AddSeconds(3); // immediate restart
Notify.RunAppAtTime(thisName, time);   // restart the app

Note that although the time is set to 3 seconds from now it will restart immediately. To have a real delay the time difference must be higher than 10 seconds. More on that here: https://stackoverflow.com/a/6133136/3330348

Community
  • 1
  • 1
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
  • I am developing an mobile application,code for restart is working without any error but application is not restarting.Do restart function work in simulator. – Royal Jun 21 '14 at 18:08
  • My misunderstanding. I thought accessing the executable path was a problem. Sorry. As to the other question - I'm investigating :) – PiotrWolkowski Jun 21 '14 at 18:13
  • Its ok,but what about my query in comment. – Royal Jun 21 '14 at 18:13
  • I tried your code,i just commented the previous code and placed the updated code in the place where i need to restart the application.Do i need to add some other stubs.Sorry to say it is not working.On saving the settings i need to restart the application,so using RunAppAtTime is correct. – Royal Jun 21 '14 at 20:57
  • You said `RunAppAtTime` is correct - so is it working? The first bit of code should give you execution path. The second one - restart the application. They do not depend on each other. – PiotrWolkowski Jun 21 '14 at 22:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56050/discussion-between-royal-and-piotrwolkowski). – Royal Jun 22 '14 at 02:49
  • So this will restart the application after 3 secs the current application shut down-right.Application.Exit() should comes after the restart code or before the restart code.Please clarify. – Royal Jun 22 '14 at 15:36
  • My understanding is that the `RunAppAtTime` will restart the application immediately or if the time is higher than 10 seconds after that time. I left some links to documentation in the chat you've started. – PiotrWolkowski Jun 22 '14 at 16:32
1

An .net application can not restart itself. As it is running on Mobile only one instance is ensured by the framework, if you had targetted Windows CE, you would be able to run multiple instances.

So, RunAppAtTime is a good solution to let the app be started by the Mobile scheduler after the app itself has terminated using Application.Exit().

Another option would be a second application that is started at Application.Exit(), watches the process list to see when main applicaton is terminated (or use GetProcessExitCode), and then starts a new instance of main application. This techique is used for updaters etc.

josef
  • 5,951
  • 1
  • 13
  • 24