views:

351

answers:

1

I've got a windows mobile app, which needs to keep running to do some task in the background.

To hide the app instead of closing it was easy:

protected override void OnClosing(CancelEventArgs e)
{
  e.Cancel = true;
  this.Hide();
  base.OnClosing(e);
}

The problem: how do I show the app again, when the user starts it in the menu?
Windows Mobile does know the app is still running, so it does not start a second copy. Instead it just does nothing.

Is there any way for the app to get notified if it is started again, so the app can show it's gui?

+2  A: 

You should use this:

protected override void OnClosing(CancelEventArgs e) {
  if ( false == this.CanClose ) { // you should check, if form can be closed - in some cases you want the close application, right ;)?
    e.Cancel = true; // for event listeners know, the close is canceled
  }
  base.OnClosing(e);
  if ( false == this.CanClose ) {
    e.Cancel = true; // if some event listener change the "Cancel" property
    this.Minimize();
  }
}

The "Minimize" method should looks like in this blog post ( http://christian-helle.blogspot.com/2007/06/programmatically-minimize-application.html ).

TcKs
Ah yes, this works - the only flaw: the program keeps being visible in the task list. I'd rather have it hidden away when the user wants to close it.
Sam
I don't think, the application should hide from tasklist. Mabe the better way for your goal is split the application to the service (which will be run in background without way how to user can close it) and the application with GUI for controling the settings (etc...) which can comunicate with service and can be closed.
TcKs
I fyou want it to not appear in the "Running Programs" list at this point, simply set the Form caption to an empty string when you hide it. When it comes back to the fore, set the caption again.
ctacke
@ctacke: it's good tip. thanks
TcKs
Is there a way to create a form hidden? I want to start my app on device start, but it keeps showing the form. It would be great if I could create the form, but don't show it (just like as it was hidden using your approach).
Sam
I can have application without any form created. You can ceate the form on demand or never. However, what the sense has application without UI? If you want run some code hidden from user, you should use the service. If you want enable user to control the service, you should create alone application with UI which is able to communication with the service : look for IPC - inter process communication.
TcKs