Click here to Skip to main content
15,891,473 members
Articles / Desktop Programming / WPF
Tip/Trick

Safe WPF Battery Charger

Rate me:
Please Sign up or sign in to vote.
4.68/5 (7 votes)
4 Apr 2014CPOL1 min read 13.1K   359   8   2
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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Ethiopia Ethiopia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProject Won't Build Pin
kccodr6-Apr-14 9:56
professionalkccodr6-Apr-14 9:56 
AnswerRe: Project Won't Build Pin
Tesfamichael G.6-Apr-14 23:21
Tesfamichael G.6-Apr-14 23:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.