Tuesday, March 06, 2007

Single instance application, screen saver and power management disabling and battery meter

The first client's request was to make my WPF application single-instance based. Really simple, remember from WinForms? Named pipes to itself, mutexes, VB approach. First method is rather complicated, and I do not want to put all this code into my app.xaml.cs file. The second one is rather good, but it has security issues, so the only reasonable way to do it is using VB's WindowsFormsApplicationBase approach.

How? Please, before asking me, take a brief look into Windows SDK. One of samples there speaking about it. Anyhow, all you should do is to create Manager, derived from WindowsFormsApplicationBase (only this class has IsSingleInstance property). All other stuff is really straight forward.

Delete your app.xaml (with cs). The create new cs class and put start point there (as well as you do it in 2.0 and 1.1 application


public class AppManager
     public static void Main(string[] args)
           ApplicationManager manager = new ApplicationManager();


Then create new class, derrived from WindowsFormsApplicationBase (don't forget to add reference to Microsoft.VisualBasic) inside the file (or in other file if you wish and override OnStartup


public class ApplicationManager : WindowsFormsApplicationBase
   ManagedApplication app;

   public ApplicationManager()
      this.IsSingleInstance = true;

   protected override bool OnStartup    (Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
     app = new ManagedApplication();
     return false;

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)

Next create application derived class to wake or run your application


public class ManagedApplication : Application
   protected override void OnStartup(System.Windows.StartupEventArgs e)

Window1 wnd = new Window1();

internal void Activate()
if (!this.MainWindow.IsVisible)

if (this.MainWindow.WindowState == WindowState.Minimized)
this.MainWindow.WindowState = WindowState.Normal;


That's all, folks. Now your application will run only once. Each other instance will wake, restore and activate your program


The next challenge is to prevent screen saver and power management from appear, while my application is running. To do it, we'll have to go deeper into WinProc and make some woodoo on system messages (don't you did it recently)

In you main window subscribe to SourceInitialized to get your application handler (the only handler in WPF application)


public Window1()
SourceInitialized += new EventHandler(Window1_SourceInitialized);

Then, get handle and hook on it's messages

void Window1_SourceInitialized(object sender, EventArgs e)

Messages you need is WM_SYSCOMMAND at the beginning, and then SC_SCREENSAVE and SC_MONITORPOWER. Simple, isn't it? The only thing you should remember, that in WPF we can use only 4 low order bits from wParam (read MSDN) so, our hook will looks like this


const int WM_SYSCOMMAND = 0x112;
const int SC_SCREENSAVE = 0xF140;
const int SC_MONITORPOWER = 0xF170;

//we need only 4 low order bits of the wParam
const int lowOrderMask = 0xFFF0;
IntPtr myHook(IntPtr hwnd, int mgs, IntPtr wParam, IntPtr lParam, ref bool handled)
if (mgs == WM_SYSCOMMAND &&
((((long)wParam & lowOrderMask) == SC_SCREENSAVE) ||
((long)wParam & lowOrderMask) == SC_MONITORPOWER))
handled = true;
return IntPtr.Zero;

Remember, that you are working with laptop and you really need power. So, let's make battery monitor for fun.

In order to get battery status we'll make PInvoke for GetSystemPowerStatus from kernel. I will not explain how to do it (you can see it in the sample attached), but I want to use WPF power to show it. We'll do DependencyObject with a couple of read only dependency properties.

public class Battery : DependencyObject

So, we have one. Let's put timer and get the battery status every second


public Battery()
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = TimeSpan.FromSeconds(1);

Now we need read only dependency property. For this we'll have to create DependencyPropertyKey first. Only one, who can access those keys will able to change the value of the property. Everything else is really simple and looks exactly like standard DP.


public static readonly DependencyPropertyKey BatteryStatusPropertyKey = DependencyProperty.RegisterReadOnly
("LineStatus", typeof(ACLineStatus), typeof(Battery),
new UIPropertyMetadata(ACLineStatus.Unknown));

public static DependencyProperty BatteryStatusProperty = BatteryStatusPropertyKey.DependencyProperty;

public ACLineStatus LineStatus
get { return (ACLineStatus)GetValue(BatteryStatusProperty); }

So, as you can see, we made readonly DP of ACLineStatus tyle and we'll fill it this way SetValue(BatteryStatusPropertyKey, (ACLineStatus)m_status.ACLineStatus); later. We'll can access the key, so we can set values, all others will be able only get.

Next we'll do it for all other properties and make simple graphic indicator for this. This one:


<Border BorderThickness="2" BorderBrush="Black" Height="100">
<VisualBrush Stretch="None" AlignmentX="Left">
<Rectangle Fill="Red" Height="100">
<MultiBinding Converter="{StaticResource pcConv}">
<Binding Source="{StaticResource battery}"
<Binding ElementName="myWindow" Path="Width"/>

Now the rectangle visual as background brush will fill the relevant space according the battery life. We'll have to scale it for window width change, so, we'll bind to Width of out window and BatteryLifePercent readonly DP. The converter do following


class PercCalcConverter:IMultiValueConverter
#region IMultiValueConverter Members

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
double pr = (double)values[0];
double max = (double)values[1];

double val = pr * max / 100;
return val;

That's all, for today, folks. See the attachment with full application source (as usual) with a little surprise inside it. Going sleep now...

Source code for this article

1 comment:

Roger Martin said...

Thanks for the code sample for setting up a single instance app. Just what I was looking for, and worked better than the other sample I tried.