Reputation: 20926
I read many forums and links like:
How do I restart my C# WinForm Application?
but all this are for windows not for mobile. How can i restart my mobile application when i catch unhandled exception. Now app is closed when i catch unhandled exception but i do not want to close it so i want to restart it.
This not working:
ProcessStartInfo s = new ProcessStartInfo();
s.FileName = Assembly.GetExecutingAssembly().GetName().CodeBase;
s.UseShellExecute = false;
Process.Start(s);
i am uisng windows mobile 6.1 and CF 3.5
Upvotes: 0
Views: 2252
Reputation: 1857
You shouldn't. The best approach is to prevent that exception with fixing the bug that causes it in the first place.
But if you really, really, really need to restart your application for any other reason than crashes, then you can start another process in the background along with your application that watches if the application is still running and starts it with Process.Start()
again if it doesn't.
(If your application is set to run automatically on the device and your device supports it, -which is not very likely since it's a Windows CE thing, but why not- you can also create Watchdog timers to restart the entire device if needed.)
But restarting an application all the time (let's say once a day) ruins the user experience and also the user's trust. So you should fix the exception instead.
Upvotes: 2
Reputation: 67188
WinMo has a mechanism baked in to prevent multiple instances of an app from running, so calling yourself with the Process class will simply launch an instance that the shell terminates. You have to work around that, P/Invoke CeRunAppAtTime
or create a separate watchdog process that makes sure your app process is always running, launching a new instance if it detects it is ever gone.. Be forewarned that CeRunAppAtTime
can't schedule an app for less than 11 seconds into the future, so there will be a delay.
Upvotes: 4