c# countdown timer pause - c#

I've been trying to create a countdown timer(from 30 minutes) in a c# winforms application that has the ability to pause, and then resume from where the pause took place. I've tried a number of solutions to achieve this as I know there isn't a specific pause function of the System.Windows.Forms.Timer. I've scoured the internet but couldn't find anything that I could apply to my scenario. Everything I've tried either results in the timer starting again from 30 minutes, or continuing from where it would have been without a pause. This is driving me mad. Can anybody help or suggest an alternative way of doing this? This is my first post here so apologies if I've made any errors. Code below. I've commented out the code that is causing me an issue - I know it's not correct, either syntactically or logically.
public partial class FormWithTimer : Form
{
System.Windows.Forms.Timer timerx = new System.Windows.Forms.Timer();
DateTime startTime = DateTime.Now;
DateTime stopTime = DateTime.Now;
public FormWithTimer()
{
InitializeComponent();
TimerLabel.Text = "30:00";
}
private void Form1_Load(object sender, EventArgs e)
{
}
void timer_Tick(object sender, EventArgs e)
{
}
public void StartButton_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
BeginTextBox.Text = startTime.ToString();
TimerLabel.Visible = true;
timerx.Tick += (obj, args) =>
TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - startTime)).ToString("mm\\:ss");
timerx.Enabled = true;
}
public void PauseButton_Click(object sender, EventArgs e)
{
if (PauseButton.Text == "Pause")
{
timerx.Stop();
PauseButton.Text = "Start";
stopTime = DateTime.Now;
}
else
{
PauseButton.Text = "Pause";
timerx.Start();
TimerLabel.Visible = true;
//timerx.Tick += (obj, args) =>
// TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - (startTime - stopTime))).ToString("mm\\:ss");
timerx.Enabled = true;
}
}
}

I guess you are trying to have the timer continue where it was when it got paused.
To do that, I would suggest using stopTime for that matter:
In your "Pause-Unpause"- Routine:
public void PauseButton_Click(object sender, EventArgs e)
{
if (PauseButton.Text == "Pause")
{
timerx.Stop();
PauseButton.Text = "Start";
stopTime = DateTime.Now;
}
else
{
PauseButton.Text = "Pause";
startTime += (DateTime.Now - stopTime);
timerx.Start();
TimerLabel.Visible = true;
timerx.Enabled = true;
}
}

Related

How can I send a message box if text hasn't been saved within X mins and the user wants to close the app?

Here's the pseudo code:
private void ForgetSave()
{
if (the SaveRegularly method hasn't been used within 3 mins)
MessageBox.Show("Would you like to save any changes before closing?")
......... the code continues.
}
else
{
this.close();
}
Does anybody know how to write the first line of the if statement?
Simply remember when the last save time was:
private const TimeSpan saveTimeBeforeWarning = new TimeSpan(0,1,0); //1 minute
private static DateTime _lastSave = DateTime.Now;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if ((DateTime.Now - _lastSave) > saveTimeBeforeWarning)
{
if(MessageBox.Show("Would you like to save any changes before closing?") == DialogResult.Yes);
{
Save();
}
}
}
private void Save()
{
//save data
_lastSave = DateTime.Now
}
As Ahmed suggested you can use a timer and a flag to know when you have to display the message, I left you a piece of code to get you started
private const int SAVE_TIME_INTERVAL = 3 * 60 * 1000;
private bool iWasSavedInTheLastInterval = true;
private System.Windows.Forms.Timer timer;
public Form1()
{
InitializeComponent();
//Initialize the timer to your desired waiting interval
timer = new System.Windows.Forms.Timer();
timer.Interval = SAVE_TIME_INTERVAL;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
//If the timer counts that amount of time we haven't saved in that period of time
iWasSavedInTheLastInterval = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (iWasSavedInTheLastInterval == false)
{
MessageBox.Show("Would you like to save any changes before closing?");
}
}
private void btnSave_Click(object sender, EventArgs e)
{
//If a manual save comes in then we restart the timer and set the flag to true
iWasSavedInTheLastInterval = true;
timer.Stop();
timer.Start();
}

c# combo box with time for timer to elapse

I have a program that disables the lockscreen and stop a service in windows. I have two buttons Enable,Disable and a combo box that has preset times. When My program is ran and the user clicks Enable the program should disable lock screen until the user manually
clicks disable. What I am trying to accomplish is to keep the program from running all night long if user never hits disable. So by selecting a preset time out of the combo box the program will auto disable it self.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DateTime time = DateTime.Today;
for (DateTime _time = time.AddHours(16); _time < time.AddHours(18); _time = _time.AddMinutes(30))
{
comboBox1.Items.Add(_time.ToShortTimeString());
}
}
private static System.Timers.Timer _Timer;
private DateTime _lastRun = DateTime.Now;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string strTime_Start = DateTime.Today.ToString();
string strTime_End = comboBox1.SelectedItem.ToString();
}
public void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
_Timer = new System.Timers.Timer(10 * 60 * 1000);
_Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
DisableLock();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (strTime_End < DateTime.Now.Date) //I think this would be where I need to have strTime_End?
{
_Timer.Stop();
_lastRun = DateTime.Now;
}
}
}
The simplest solution in my mind would be to keep an instance variable for your stop time, and each combobox item you have sets this stop time, the timer_tick event would simply check if it has passed that time. A blank item in the combo box can clear the variable.
private DateTime timeToStop;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
timeToStop = DateTime.Now.Add(DateTime.Parse(comboBox1.Text));
}
catch(Exception)
{
timeToStop = new DateTime(3000, 01, 01, 00, 00, 00);
}
}
public void disableButton_Click(object sender, EventArgs e)
{
_Timer.Stop();
_lastRun = DateTime.Now;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now >= timeToStop)
{
_Timer.Stop();
_lastRun = DateTime.Now;
// Disable regkey
}
}
from what i understood so far, you can just add :
comboBox1.Enabled = false;
when the time is elapsed, i.e. in the event.

C#, Constantly monitor battery level

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.

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!

How to increment in time?

I am creating a stopwatch type application. I've used a label to display time. When application starts, the label is initialized to '00:00:00' and I want to increment its time by every 1 second.
I am trying to accomplish this job by using timer.
In your timer get the system time so your timer must be with very small interval like 200ms. To calculate your time just calculate the currentTime - startTIme in seconds.
If I am getting you right, it may surely work. Set initial label Text as "00:00:00". Set timer interval as 1000.
private void btnStartWatch_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void btnPauseWatch_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
int i = 1;
DateTime dt = new DateTime();
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = dt.AddSeconds(i).ToString("HH:mm:ss");
i++;
}
Hope it helps.
You want the TimeSpan structure.
Something like:
TimeSpan current = new TimeSpan(0);
// In your update loop:
current += TimeSpan.FromSeconds(1);
I have a timer on one of my apps.
private int _seconds;
public string TimeDisplay
{
get
{
TimeSpan ts = new TimeSpan( 0, 0, _seconds );
return string.Format("{0,2:00}:{1,2:00}:{2,2:00}", ts.Minutes, ts.Minutes, ts.Seconds);
}
}
All you have to do is have your timer_tick even increment _seconds and NotifyPropertyChanged() if you're binding to it. Either way, TimeDisplay will have your result.
i hope understand.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
Timer1.Interval = 1000;
Timer1.Start();
}
private void Timer1_Tick(System.Object sender, System.EventArgs e)
{
this.Label1.Text = DateTime.Now.ToString("hh/mm/ss");
}
Regards

Categories