Answered by:
timer

Question
-
hello everyone,
so im working on a project where the user enters time in mm:ss format in a editable listview, then the program is sapposed to do some math to countdown the amount of time entered ounce the start button is pressed. my problem is im doing the timer calcualtion in background worker and everytime the backround worker runs it rereads the input from the listview causing the math to always be wrong.
here is my code
que is set to 1 when a button is pressed my thinking was that i could only set the value for the timer when the program starts then stop rereading it from the listview when a press a button which sets que to 1 but the program will not compile if i dont set some value "end" in every part of the switch statement, i tried the same thing with a "if else" statement
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string Time = listView1.Items[0].Text;
DateTime dateTime = DateTime.ParseExact(Time, "mm:ss",
CultureInfo.InvariantCulture);
DateTime end;
DateTime start;
int min, sec;
switch(que)
{
case 1:
start = DateTime.Now;
end = DateTime.Now.AddMinutes(dateTime.Minute);
end = end.AddSeconds(dateTime.Second);
break;
case 2:
end = DateTime.Now.AddMinutes(dateTime.Minute);
end = end.AddSeconds(dateTime.Second);
break;
default:
end = DateTime.Now.AddMinutes(dateTime.Minute);
end = end.AddSeconds(dateTime.Second);
break;
}
min = (int)(dateTime - end).Minutes;
sec = (int)(dateTime - end).Seconds;
label1.Text = min + ":" + sec;// dateTime.ToString("mm:ss", CultureInfo.CurrentCulture);
}thanks everyone!
- Moved by CoolDadTx Tuesday, December 1, 2015 2:36 PM Winforms related
Tuesday, December 1, 2015 4:45 AM
Answers
-
Do you need a backgroundworker?
As this is a windows forms application, it might be easiest to use a System.Windows.Forms.Timer. You can set it up to do something every second (as in below example).
public partial class Form1 : Form { // aa timer for count down Timer tmr = new Timer(); public Form1() { // this is standard in a Windows form InitializeComponent(); // setup timer with interval of one second tmr.Interval = 1000; // and add event handler tmr.Tick += tmr_Tick; }
// timer event handler void tmr_Tick(object sender, EventArgs e) { // split the time specified in a textbox string[] time = textBox1.Text.Split(':'); // convert minutes and seconds int minutes = Convert.ToInt32(time[0]); int seconds = Convert.ToInt32(time[1]); // create a timespan from it TimeSpan ts = new TimeSpan(0, minutes,seconds); // create a timespan representing one second TimeSpan tsOnesecond = new TimeSpan(0,0,1); // subtract one second from current timespan ts = ts.Add(tsOnesecond.Negate()); // display new time in textbox textBox1.Text = String.Format("{0:D2}:{1:D2}", ts.Minutes, ts.Seconds); // check if there is still time left if (ts.Ticks == 0) { // we're done, stop timer, inform user and re-enable start button tmr.Stop(); MessageBox.Show("Done"); btnStart.Enabled = true; } }
// start button event handler private void btnStart_Click(object sender, EventArgs e) { // set a duration value textBox1.Text = "00:30"; // start timer tmr.Start(); // disable start button btnStart.Enabled = false; } }
This just shows the basics;
You probably need a global variable to remember the time left (use the TimeSpan for that); the above code remembers it by using the textbox what is more than likely not what you want. In that case you can initialize a global TimeSpan variable in the start button's event handler and subtract a second in the timer's event handler.
- Edited by sterretje_K5 Tuesday, December 1, 2015 5:43 AM
- Proposed as answer by Herro wongMicrosoft contingent staff Monday, December 14, 2015 5:12 AM
- Marked as answer by Herro wongMicrosoft contingent staff Tuesday, December 15, 2015 2:10 AM
Tuesday, December 1, 2015 5:40 AM -
i am exactly not sure following you.
do you want to like as below i guess?
1. if user press the the button and set the variable que to 1
2.do the time calculation operation in the background-work process event when variable que == 1
if that is your expect for. you just need do like below using if statement
string Time = listView1.Items[0].Text; DateTime dateTime = DateTime.ParseExact(Time, "mm:ss", CultureInfo.InvariantCulture); if(que==1) { DateTime start = DateTime.Now; DateTime end = start; end = end.AddMinutes(dateTime.Minute); end = end.AddSeconds(dateTime.Second); int min = (int)(dateTime - end).Minutes; int sec = (int)(dateTime - end).Seconds; label1.Text = min + ":" + sec;// dateTime.ToString("mm:ss", CultureInfo.CurrentCulture); }
DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.
- Proposed as answer by Herro wongMicrosoft contingent staff Monday, December 14, 2015 5:12 AM
- Marked as answer by Herro wongMicrosoft contingent staff Tuesday, December 15, 2015 2:10 AM
Tuesday, December 1, 2015 9:47 AM
All replies
-
Do you need a backgroundworker?
As this is a windows forms application, it might be easiest to use a System.Windows.Forms.Timer. You can set it up to do something every second (as in below example).
public partial class Form1 : Form { // aa timer for count down Timer tmr = new Timer(); public Form1() { // this is standard in a Windows form InitializeComponent(); // setup timer with interval of one second tmr.Interval = 1000; // and add event handler tmr.Tick += tmr_Tick; }
// timer event handler void tmr_Tick(object sender, EventArgs e) { // split the time specified in a textbox string[] time = textBox1.Text.Split(':'); // convert minutes and seconds int minutes = Convert.ToInt32(time[0]); int seconds = Convert.ToInt32(time[1]); // create a timespan from it TimeSpan ts = new TimeSpan(0, minutes,seconds); // create a timespan representing one second TimeSpan tsOnesecond = new TimeSpan(0,0,1); // subtract one second from current timespan ts = ts.Add(tsOnesecond.Negate()); // display new time in textbox textBox1.Text = String.Format("{0:D2}:{1:D2}", ts.Minutes, ts.Seconds); // check if there is still time left if (ts.Ticks == 0) { // we're done, stop timer, inform user and re-enable start button tmr.Stop(); MessageBox.Show("Done"); btnStart.Enabled = true; } }
// start button event handler private void btnStart_Click(object sender, EventArgs e) { // set a duration value textBox1.Text = "00:30"; // start timer tmr.Start(); // disable start button btnStart.Enabled = false; } }
This just shows the basics;
You probably need a global variable to remember the time left (use the TimeSpan for that); the above code remembers it by using the textbox what is more than likely not what you want. In that case you can initialize a global TimeSpan variable in the start button's event handler and subtract a second in the timer's event handler.
- Edited by sterretje_K5 Tuesday, December 1, 2015 5:43 AM
- Proposed as answer by Herro wongMicrosoft contingent staff Monday, December 14, 2015 5:12 AM
- Marked as answer by Herro wongMicrosoft contingent staff Tuesday, December 15, 2015 2:10 AM
Tuesday, December 1, 2015 5:40 AM -
i am exactly not sure following you.
do you want to like as below i guess?
1. if user press the the button and set the variable que to 1
2.do the time calculation operation in the background-work process event when variable que == 1
if that is your expect for. you just need do like below using if statement
string Time = listView1.Items[0].Text; DateTime dateTime = DateTime.ParseExact(Time, "mm:ss", CultureInfo.InvariantCulture); if(que==1) { DateTime start = DateTime.Now; DateTime end = start; end = end.AddMinutes(dateTime.Minute); end = end.AddSeconds(dateTime.Second); int min = (int)(dateTime - end).Minutes; int sec = (int)(dateTime - end).Seconds; label1.Text = min + ":" + sec;// dateTime.ToString("mm:ss", CultureInfo.CurrentCulture); }
DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.
- Proposed as answer by Herro wongMicrosoft contingent staff Monday, December 14, 2015 5:12 AM
- Marked as answer by Herro wongMicrosoft contingent staff Tuesday, December 15, 2015 2:10 AM
Tuesday, December 1, 2015 9:47 AM