Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Safe WPF Battery Charger

0.00/5 (No votes)
4 Apr 2014 1  
Program that notifies if battery is overcharging

Figure 1. The Application

Introduction

I bought HP laptop 4 years ago and it was clearly stated in the manual that I should not overcharge the battery. Its life time was at least 3-4 hrs. One day, I was working while it was charging and the sleep took over me only to find out that the battery was being charged the whole night.

By this time, the life time was reduced to 1-2 hrs. I was then guilty on many occasions for forgetting to disconnect the charger when it was fully charged.

Before I use it for a year, it went down to 20 minutes life time and then after a year to zero.

Last year, I bought another laptop with 5 hrs battery life time; hence not to repeat the same mistake I had to do something. CREATE A SAFE CHARGER WPF APPLICATION.

Background

I decided to use Notify Control and the article by Murray Foxcroft was very helpful. I didn't want to use the Extended Notify Control, however I liked his touch on making the user experience to be slick and smooth.

Thanks to the .NET, you can get every information you need about your battery through System.Windows.Forms.SystemInformation.PowerStatus.

Figure 2. The System.Windows.Forms.SystemInformation.PowerStatus

Using the Code

The application has the following components:

  1. Notify Control - sits on the task bar
  2. Notify window - pops up when warning
  3. Settings window - for setting user options

The application's OnStartup method is implemented as follows:

protected override void OnStartup(StartupEventArgs e)
{
    Debug.Print("OnStartup==>starting . . ." );
    base.OnStartup(e);
    NotifyIcon.Visible = true;
 
    NotifyIcon.Click +=  delegate(object s, EventArgs ea)
    {
        DataSource.ForceShow = !DataSource.ForceShow;
        NotifierWindow.Show(); 
    };
    Debug.Print("OnStartup==>started.");
 
    Startup += new StartupEventHandler(App_Startup);
 
    NotifyIcon.Text = DataSource.TimeLeft; ;
}

The Usersettings class which is the data source, has a dispatcher timer whose tick event is handled in the following manner:

void handleTimer_Tick(object sender, EventArgs e)
{
    NotifyPropertyChanged("CurrentLevel");
    NotifyPropertyChanged("TimeLeft");
    NotifyPropertyChanged("CurrentStatus");
    NotifyPropertyChanged("ForceShow");
    NotifyPropertyChanged("IsOnline");
    NotifyPropertyChanged("Warn");
 
    App.NotifyIcon.Text = TimeLeft;
    if (Warn)
    {
        if (!IsPlaying)
        {
            player.Play();
            IsPlaying = true;
        }
        Console.Beep();
    }
    else
    {
        if (IsPlaying)
        {
            Player.Stop();
            IsPlaying = false;
        }
    }
}

Here, the Player is a MediaElement object.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here