Sunday, July 06, 2008

Tip: Bringing a .NET CF Application to the Foreground

In the .NET CF, there is no API to easily bring your application to the foreground. For example, your application may be intercepting incoming SMS messages and is switched to the background when the user uses other applications. When a SMS message is received, you often need to bring your application to the foreground so that the user is notified of the event. To do so, you need to understand that Windows Mobile applications are single-instance. That is, only a single instance of an application can run at a time.

To bring the application to the foreground, first use reflection to find the path of the current application. Then use the Process.Start() method to run the application again. Since Windows Mobile applications are single-instance, using the Start() method on an already running application simply brings it to the foreground:

String appPath =
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
System.Diagnostics.Process.Start(appPath, "");

No comments: