C#, Constantly monitor battery level - c#

I am designing a program that depends on monitoring the battery level of the computer.
This is the C# code I am using:
PowerStatus pw = SystemInformation.PowerStatus;
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
My failed attempt of the while statement, it uses all the CPU which is undesirable.
int i = 1;
while (i == 1)
{
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
How do I monitor this constantly with an infinite loop so that when it reaches 75% it will execute some code.

Try Timer:
public class Monitoring
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public Monitoring()
{
timer1.Interval = 1000; //Period of Tick
timer1.Tick += timer1_Tick;
}
private void timer1_Tick(object sender, EventArgs e)
{
CheckBatteryStatus();
}
private void CheckBatteryStatus()
{
PowerStatus pw = SystemInformation.PowerStatus;
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
}
UPDATE:
There is another way to do your task complete. You can use SystemEvents.PowerModeChanged.
Call it and wait for changes, monitor the changes occured then do your stuff.
static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
{
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
}

While loop will cause your UI to response poor and the application will get crashed. You can solve this by using many ways. Please check out the below code snippet will help your needs.
public delegate void DoAsync();
private void button1_Click(object sender, EventArgs e)
{
DoAsync async = new DoAsync(GetBatteryDetails);
async.BeginInvoke(null, null);
}
public void GetBatteryDetails()
{
int i = 0;
PowerStatus ps = SystemInformation.PowerStatus;
while (true)
{
if (this.InvokeRequired)
this.Invoke(new Action(() => this.Text = ps.BatteryLifePercent.ToString() + i.ToString()));
else
this.Text = ps.BatteryLifePercent.ToString() + i.ToString();
i++;
}
}

BatteryChargeStatus.Text = SystemInformation.PowerStatus.BatteryChargeStatus.ToString();
BatteryFullLifetime.Text = SystemInformation.PowerStatus.BatteryFullLifetime.ToString();
BatteryLifePercent.Text = SystemInformation.PowerStatus.BatteryLifePercent.ToString();
BatteryLifeRemaining.Text = SystemInformation.PowerStatus.BatteryLifeRemaining.ToString();
PowerLineStatus.Text = SystemInformation.PowerStatus.PowerLineStatus.ToString();
If you want to perform some operation just convert these string values into the integer.

Related

C# Using task and yield to keep UI informed of a running process

Is it bad practice to write code like this. What I want to accomplish is that a user can press a button on a control. The button starts some kind of analyzing process and for each item done it shows a result to the user.
private IEnumerable<int> AnalyzeItems() {
for(int i = 0; i < 1000; i++) {
Thread.Sleep(500);
yield return i;
}
}
private void PerformTask_Click(object sender, EventArgs e) {
Task.Run(() => {
foreach (var item in AnalyzeItems()) {
ResultLog.Invoke((Action)delegate() { ResultLog.Text += item.ToString(); });
}
});
}
why do not use Backgroundworker?
First setup the backgroundworker properties to:
WorkerReportsProgress = true
WorkerSupportsCancellation = true
This is the code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 1000; i++) {
Thread.Sleep(500);
if (backgroundWorker1.CancellationPending) {
e.Cancel = true;
break;
}
backgroundWorker1.ReportProgress(i / 10, "step " + i);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
label1.Text = e.UserState.ToString();
progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e) {
cancelButton.Focus();
button1.Enabled = false;
backgroundWorker1.RunWorkerAsync();
}
private void cancelButton_Click(object sender, EventArgs e) {
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
button1.Enabled = true;
if (e.Error != null) {
MessageBox.Show(e.Error.Message, "Unexpected error");
}
if (e.Cancelled) {
MessageBox.Show("Process stopped by the user", "Cancelled");
}
label1.Text = "Press start";
progressBar1.Value = progressBar1.Minimum;
}
}
Is your approach bad practice? It depends.
If you don't expect your code inside Task.Run to throw any exceptions and you want to continue doing something else, then your code is ok. However, if you want to capture any possible exceptions and wait for the process to finish without freezing UI, then you might want to consider using async/await.
private async void PerformTask_Click(object sender, EventArgs e) {
try
{
await Task.Run(() => {
foreach (var item in AnalyzeItems()) {
ResultLog.Invoke((Action)delegate() { ResultLog.Text += item.ToString(); });
}
});
}
catch(Exception ex)
{
// handle...
}
}
Alternative approach would be to use IProgress<T>. This allows for easy separation of long running work and updating UI. Please note that you shouldn't call this method too often, because
This will put too much work on UI thread resulting in UI freeze.
If you pass any valuetype to IProgress<T>.Report method, then it gets copied. If you call this too often, you risk running garbage collector very often resulting in even bigger freezes.
All of this means that you should utilize IProgress only for truly long running work.
Now that we have it all out of the way, here is a sample of how you could notify users about progress of analyzed items:
private double _currentProgress;
public double CurrentProgress {
get => _currentProgress;
set
{
_currentProgress = value;
NotifyPropertyChanged();
}
}
private async void PerformTask_Click(object sender, EventArgs e)
{
var progress = new Progress<double>();
progress.ProgressChanged += (sender, p) => CurrentProgress = p;
await Task.Run(() => AnalyzeItems(Enumerable.Range(0, 5000).ToList(), progress));
}
private void AnalyzeItems(List<int> items, IProgress<double> progress)
{
for (int itemIndex = 0; itemIndex < items.Count; itemIndex++)
{
// Very long running CPU work.
// ...
progress.Report((double)itemIndex * 100 / items.Count);
}
}
If AnalyzeItems takes less than 100 ms for individual item, then you don't want to report after every finished item (see why above). You can decide how often you want to update status like this:
private void AnalyzeItems(List<int> items, IProgress<double> progress)
{
var lastReport = DateTime.UtcNow;
for (int itemIndex = 0; itemIndex < items.Count; itemIndex++)
{
// Very long running work.
Thread.Sleep(10);
// Tell the user what the current status is every 500 milliseconds.
if (DateTime.UtcNow - lastReport > TimeSpan.FromMilliseconds(500))
{
progress.Report((double)itemIndex * 100 / items.Count);
lastReport = DateTime.UtcNow;
}
}
}
If you have really a lot of very fast iterations, you may want to consider changing DateTime.Now to something else.

How can I cancel backgroundWorker in specific case and start the backgroundWorker over?

In form1 top:
ExtractImages ei = new ExtractImages();
In form1 constructor i start the backgroundworker first time
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
}
In dowork event
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (backgroundWorker1.CancellationPending == true)
{
e.Cancel = true;
return; // this will fall to the finally and close everything
}
else
{
ei.ProgressChanged += (senders, eee) => backgroundWorker1.ReportProgress(eee.Percentage, eee.StateText);
ei.Init();
}
}
In progresschanged
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Value = e.ProgressPercentage;
label7.Text = e.UserState.ToString();
label8.Text = e.ProgressPercentage + "%";
}
In completed
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null) { ProgressBar1.Value = 100; }
else
{
}
}
In the class top
public bool WebProblem = false;
public class ProgressEventArgs : EventArgs
{
public int Percentage { get; set; }
public string StateText { get; set; }
}
public event EventHandler<ProgressEventArgs> ProgressChanged;
public void Init()
{
object obj = null;
int index = 0;
ExtractCountires();
foreach (string cc in countriescodes)
{
// raise event here
ProgressChanged?.Invoke(obj, new ProgressEventArgs { Percentage = 100 * index / countriescodes.Count, StateText = cc });
ExtractDateAndTime("http://www.sat24.com/image2.ashx?region=" + cc);
index += 1;
}
ImagesLinks();
}
Method in class
public void ExtractDateAndTime(string baseAddress)
{
try
{
var wc = new WebClient();
wc.BaseAddress = baseAddress;
HtmlDocument doc = new HtmlDocument();
var temp = wc.DownloadData("/en");
doc.Load(new MemoryStream(temp));
var secTokenScript = doc.DocumentNode.Descendants()
.Where(e =>
String.Compare(e.Name, "script", true) == 0 &&
String.Compare(e.ParentNode.Name, "div", true) == 0 &&
e.InnerText.Length > 0 &&
e.InnerText.Trim().StartsWith("var region")
).FirstOrDefault().InnerText;
var securityToken = secTokenScript;
securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));
securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", "");
var dates = securityToken.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
var scriptDates = dates.Select(x => new ScriptDate { DateString = x });
foreach (var date in scriptDates)
{
DatesAndTimes.Add(date.DateString);
}
}
catch(WebException wex)
{
WebProblem = true;
}
}
What i want to do is once the WebProblem is true stop the loop in the Init() and stop/cancel the backgroundworker in form1.
Then in the backgroundworker completed event throw a message to a label about the problem. And start a timer and after 30 seconds start the backgroundworker again.
If the exception happen and i will use a break point in the catch and click continue it will continue but i want that once there was a problem stop everything and start over again until it will pass without any problem in the middle.
A lot of really peculiar stuff there. I suggest starting with a BackgroundWorker tutorial. Suggestions about what you have posted:
The form constructor is a really bad place to call .RunWorkerAsync; especially if you want to be able to restart the background process again. If you really want to start your BackgroundWorker immediately, you should call something like this in your Form_Load method:
backgroundWorker1.RunWorkerAsync(new Uri("http://www.sat24.com/image2.ashx"));
Your DoWorkEventHandler is where you should do your background work, check for cancellation, and report results and you aren't doing any of those. You want something like:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var result = new List<DateString>();
var baseUri = (Uri)e.Argument;
var countryCodes = ExtractCountries();
foreach (var cc in countryCodes)
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
result.Add(ExtractDateAndTime(new Uri(baseUri, "?region=" + cc));
}
e.Result = result;
}
You want ExtractCountries to return an IEnumerable of whatever data you want to enumaerate, and ExtractDateAndTime to accept a complete Uri and return a list of the results. Do not embed that logic in the methods or use globals to return the results; everything you need is already a part of BackgroundWorker, do not create a new class just to hand off the data. Your ExtractDateAndTime declaration should look like this:
private DateString ExtractDateAndTime(Uri source)
Do not catch exceptions in ExtractDateAndTime; doing that will discard the exception and continue as if nothing was wrong, then return the wrong answer without warning. If you want to display the exception, let it propagate to the RunWorkerCompleted event and inspect RunWorkerCompletedEventArgs.Error.
If you want to restart the BackgroundWorker if you get an error you want to declare a Form.Timer in your Design view or Form constructor and a Tick EventHandler something like:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
backgroundWorker1.RunWorkerAsync(new Uri("http://www.sat24.com/image2.ashx"));
}
In your RunWorkerCompletedEventHandler do something like:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
AlertUserSomehow(e.Error);
timer1.Interval = 30000;
timer1.Start();
return;
}
DoSomethingWith((List<DateString>)e.Result);
}

replacement for thread.sleep

I'm trying to make a Tamagochi but I've ran into a problem. I have a Progressbar with a max value of 300. Every 5-8 seconds the Progressbar empties a bit. Once it gets below 250 you're allowed to sleep.
Here is the code i have so far:
private void BtnSleep_Click(object sender, EventArgs e)
{
if (PgbSleep.Value <= 250)
{
int temp = PgbSleep.Maximum - PgbSleep.Value;
if (temp + PgbSleep.Value >= 300)
{
Timer2.Stop();
Thread.Sleep(20000);
PgbSleep.Value = 300;
Timer2.Start();
}
}
else
{
MessageBox.Show("Your pokemon is not tired enough to sleep! try playing with it");
}
}
I'm trying to find a replacement for the
Thread.Sleep(20000);
But dont know what to use. Any help would be much appreciated!
The
Thread.Sleep(20000);
Is supposed to be a cooldown, once its completed the user is allowed to sleep again if the requirements are met.
Try using a timer:
Timer sleepTimer = new Timer(20000); //Creates a timer for sleeping
public MyClass()
{
sleepTimer.Elapsed += new EventHandler((s, e) => WakeUp());
}
private void BtnSleep_Click(object sender, EventArgs e)
{
if (PgbSleep.Value <= 250)
{
int temp = PgbSleep.Maximum - PgbSleep.Value;
if (temp + PgbSleep.Value >= 300)
{
Timer2.Stop();
sleepTimer.Start();
}
}
else
{
MessageBox.Show("Your pokemon is not tired enough to sleep! try playing with it");
}
}
private void WakeUp()
{
PgbSleep.Value = 300;
Timer2.Start();
}

C# winform application doesn't wait for all operations to finish

I'm developing a simple application that when I press the button "obtain readings", is going to listen to the microphone and process the fundamental frequency. My problem is that when I create the data to record in the DB the value is 0. If I debug the solution I can see that after the Insert the program is still processing the frequencies.
As a basis for my application I'm using the code available on Codeproject (FTGuitarTuner).
private void getReading_Click(object sender, EventArgs e)
{
StartTime = DateTime.Now;
EndTime = StartTime.AddSeconds(3);
TotalMillisegundos = EndTime.Subtract(StartTime).TotalMilliseconds;
tmr.Start();
tmr.Tick += new EventHandler(tmr_Tick);
StartListenning(device);
while (DateTime.Now <= this.EndTime)
{
isListenning = true;
tmr_Tick(sender, e);
}
StopListenning();
/*Insert na BD*/
insertReadingBD(sender, e);
getBySessaoToolStripButton2_Click(sender, e);
}
The other 3 functions are
private void StopListenning()
{
isListenning = false;
frequencyInfoSource.Stop();
frequencyInfoSource.FrequencyDetected -= new EventHandler<FrequencyDetectedEventArgs>(frequencyInfoSource_FrequencyDetected);
frequencyInfoSource = null;
}
private void StartListenning(SoundCaptureDevice device)
{
isListenning = true;
frequencyInfoSource = new SoundFrequencyInfoSource(device);
frequencyInfoSource.FrequencyDetected += new EventHandler<FrequencyDetectedEventArgs>(frequencyInfoSource_FrequencyDetected);
frequencyInfoSource.Listen();
}
void frequencyInfoSource_FrequencyDetected(object sender, FrequencyDetectedEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new EventHandler<FrequencyDetectedEventArgs>(frequencyInfoSource_FrequencyDetected), sender, e);
iEscreve = false;
}
else
{
if (e.Frequency > 25.000 && e.Frequency < 4500.000)
{
frequencies.Add(e.Frequency);
updateFreqTxt(e.Frequency);
}
}
}
My question is, how can I sync the events in a way that he only inserts in the DB after all processing is done?
Thank's!

Can't seem to get progressbar to animate

OK so I've have a problem looking like this.
public Class A{
public A(){
progressBar.Style = ProgressBarStyle.Marquee;
progressBar.MarqueeAnimationSpeed = 0;
}
public void DoSomething(){
if(checkpasses){
progressBar.MarqueeAnimationSpeed = 100;
//Do something here...
progressBar.MarqueeAnimationSpeed = 0;
}
else
//Do nothing...
}
}
The problem is that my progressbar wont start moving at all. First I figured that it wont create a new thread by itself (which I find wired) so I tried creating a thread but still the same result. Nothing happens. Is it something I've forgotten?
Call
Application.EnableVisualStyles();
at the very beginning of your application.
Your "do something here" code is going to block the UI thread so you will not see the progress bar update until after the DoSomething method completes. At that time you are setting the animation speed back to 0.
Try putting your "do something here" code on a separate thread. When that thread completes set the animation speed back to 0.
Something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.MarqueeAnimationSpeed = 0;
progressBar1.Style = ProgressBarStyle.Blocks;
progressBar1.Value = progressBar1.Minimum;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DoSomething();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 100;
backgroundWorker1.RunWorkerAsync();
}
private void DoSomething()
{
Thread.Sleep(2000);
}
}
I am not sure if this is the best solution, but I have it this way:
//this is the action item (button click)
private void importSFNFReportButton_Click(object sender, EventArgs e)
{ //I run
backgroundWorker6Progress.RunWorkerAsync(); //this is how I start the progress bar 'movement'
bgwImportSF.RunWorkerAsync(); //this is another task that is lauchned after the progress bar is initiated
}
This is actual background worker
private void backgroundWorker6Progress_DoWork(object sender, DoWorkEventArgs e)
{
bool cont = true;
while (cont)
{
PauseForMilliSeconds(100);
updateProgressbar1(false);
if (noTasksExistCheck())
{
updateProgressbar1(true);
cont = false;
}
}
}
this is a delegate- I call it to auto-increase the progress bar indicator
delegate void updateProgressBarStatus(bool done);
private void updateProgressbar1(bool done)
{
if (progressBar1.InvokeRequired)
{
updateProgressBarStatus del = new updateProgressBarStatus(updateProgressbar1);
progressBar1.Invoke(del, new object[] { done });
}
else
{
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Minimum;
}
progressBar1.PerformStep();
if (done == true)
{
progressBar1.Value = progressBar1.Minimum;
}
}
}
I control it via the function that has to check a global varibale
noTasksExistCheck()
This is the timer pause
public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
{
System.DateTime ThisMoment = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
System.DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = System.DateTime.Now;
}
return System.DateTime.Now;
}
Just to complement a bit more, the solution suggested by Dave will only work if Konstantin's suggested code exists. Otherwise, one should think of manually increasing the progressbar.value in a loop by the following code within the DoWork:
BeginInvoke(new MethodInvoker( () => progressBarSave.Value += progressBarSave.Step));

Categories