How to: Using the State and Notification broker on WM6
Sometimes we want to be notified of different events that happen in the operating system, even when we are not “looking”. the State and Notification broker is built especially for this.
I will give an example of how to be notified when the current active application changes and how to handle it.
First create the correct SystemState member:
SystemState _activeProgram = new SystemState(SystemProperty.ActiveApplication);
Now set the event handler on your program initiation:
_activeProgram.Changed += new ChangeEventHandler(_activeProgram_Changed);
void _activeProgram_Changed(object sender, ChangeEventArgs args) { MessageBox.Show(((string)args.NewValue)); }
You would want to know a few interesting things:
-
The NewValue string is formatted in this way:
PreviousActiveProgramNameCurrentActiveProgramName
So, you might want to use StartsWith\EndsWith methods in order to check them ;].
-
It seams that every UI involved action(MessageBox, Form, etc.) causes your own application to come back from the background(if it was there) and show itself ;]. while other non-UI related actions does not cause this. This only a major “feeling” i got from my little workings with this so far, i checked this with a Thread.Sleep that did not cause my application to come to the foreground.
Hope this helps.
Nadav
Tags: .net, c#, CF.Net, event handler, How to, state and notification broker, windows mobile, windows mobile 6, WM, wm6