count-down isn't working ! can anyone tell me why ?
I want to create a countdown by using for-loop, not by using timer or other built-in methods...
int totaltime, time;
private void button1_Click(object sender, EventArgs e)
{
totaltime = int.Parse(label1.Text);
time = int.Parse(label1.Text);
for (int i = totaltime; i <= 0; i--)
{
--time;
label1.Text = time.ToString();
Task.Delay(1000);
}
to see code and application s.s click here !
Change your code to
int totaltime, time;
private async void button1_Click(object sender, EventArgs e)
{
totaltime = int.Parse(label1.Text);
time = int.Parse(label1.Text);
for (int i = totaltime; i >= 0; i--)
{
--time;
label1.Text = time.ToString();
await Task.Delay(1000);
}
}
Related
I have a button to start the Timer1, the Timer1 will print the current execution time every 100ms, but when I do something calculation, the timer1_Tick() will be paused until finished that calculation, I think the calculation bother the Timer1 so that timer1_Tick() is dropped(or blocked this thread).
In fact, the calculation is very complex, maybe take 40 seconds, I just need to show the execution time every 100ms to tell user how close to the end of this function, would you please tell me how to do this work??
double a = 0;
public Form1()
{
InitializeComponent();
timer1.Interval = 100;
}
private void timer1_Tick(object sender, EventArgs e)
{
a += 0.1;
label1.Text = a.ToString();
}
private void UserStartTimer_Click(object sender, EventArgs e)
{
a = 0.0;
timer1.Enabled = true;
}
private void UserCalcSomething_Click(object sender, EventArgs e)
{
double s = 0;
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 10000; j++)
{
s = i + j;
}
}
}
private void UserStopTimer_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
Just execute the calculation in another thread or task:
private void UserCalcSomething_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => {
double s = 0;
for (int i = 0; i < 100000; i++)
{
for (int j = 0; j < 10000; j++)
{
s = i + j;
}
}
}
}
I just want to ask that if I can make a stopwatch with C# I tried:
private void button2_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
int st = 00;
int m = 00;
string stime = "00:00";
if(st == 60)
{
m++;
st = 00;
}
else
{
st++;
}
if (m == 60)
{
m = 00;
}
if(st < 10)
{
st = 0 + st;
}
if(m < 10)
{
m = 0 + m;
}
stime = m.ToString() + ":" + st.ToString();
label3.Text = stime;
}
this but it didn't worked. My timer is setted up and the interval of the timer is 1000ms. Can someone help me?
It looks to me, that you are more likly to make a watch rather than a stopwatch?
If you're making a stopwatch, I think you need a field/property in your class that holds the starting time:
private DateTime _start;
private void button2_Click(object sender, EventArgs e)
{
_start = DateTime.Now;
timer1.Start();
}
and then in timer1_Tick you can do:
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan duration = DateTime.Now - _start;
label3.Text = duration.ToString(<some format string>);
}
It seems that your current code in timer1_Tick only has local variables and therefore always will produce the same time? :-)
How do I set the maximum value of my progress bar to be the value that was input by the user in the textbox on my windows application form? Here is my current code. I need the max value to be the value that was Input. I also need it to print the current time and date.
private void btnProgNum_Click(object sender, EventArgs e)
{
progBarNum.Maximum = 10;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int i = 1;
int endProg = Convert.ToInt32(txtNumProg.Text);
while (i <= endProg )
{
System.Threading.Thread.Sleep(500);
backgroundWorker1.ReportProgress(i);
i++;
}
}
private void backgroundWorker1_ProgressChanged_1(object sender, ProgressChangedEventArgs e)
{
progBarNum.Value = e.ProgressPercentage;
lblOutProg.Text = DateTime.Now.ToString();
}
}
private void btnProgNum_Click(object sender, EventArgs e)
{
int n = int.Parse(txtNumProg.Text);
int counter = 0;
while (counter != n + 1)
{
counter++;
}
progBarNum.Maximum = n;
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.WorkerReportsProgress = true;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int i = 1;
int n = int.Parse(txtNumProg.Text);
while (i <= n )
{
System.Threading.Thread.Sleep(500);
backgroundWorker1.ReportProgress(i);
i++;
}
}
private void backgroundWorker1_ProgressChanged_1(object sender, ProgressChangedEventArgs e)
{
progBarNum.Value = e.ProgressPercentage;
lblOutProg.Text = DateTime.Now.ToString();
}
Here is a update of me fixing the problem i had with the passing a new max value for the progress bar. Now I dont think i need a second while-loop for it. it works this way, but seems like it only is ment to require one while loop.
I would like to know how to create a label that adds sum + 1 every 5 seconds? I've tried with an if loop but unfortunately it resets one second later.
Thank you in advantage for your attention
using System.Diagnostics;
// using system.diagnotics voor stopwatch
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Stopwatch sw = new Stopwatch();
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
sw.Start();
if (timer1.Enabled == true) { button1.Text = "stop"; }
else { button1.Text = "false"; sw.Stop(); }
}
private void timer1_Tick(object sender, EventArgs e)
{
int hours = sw.Elapsed.Hours;
int minutes = sw.Elapsed.Minutes;
int seconds = sw.Elapsed.Seconds;
int sum = 0;
label1.Text = hours + ":" ;
if (minutes < 10) { label1.Text += "0" + minutes + ":"; }
else { label1.Text += minutes + ":"; }
if (seconds < 10) { label1.Text += "0" + seconds ; }
else { label1.Text += seconds ; }
if (seconds ==5) { sum = sum +=1; }
label2.Text = Convert.ToString(sum);
}
}
}
sum should be a class field. Also you can use custom format string for elapsed TimeSpan.
int sum = 0;
private void timer1_Tick(object sender, EventArgs e)
{
// int sum = 0; local variable will be set to zero on each timer tick
label1.Text = sw.Elapsed.ToString(#"hh\:mm\:ss");
// btw this will not update sum each five seconds
if (sw.Elapsed.Seconds == 5)
sum++; // same as sum = sum +=1;
label2.Text = sum.ToString();
}
Your current implementation will increase sum only if current elapsed timeout's second value is five. Which could never happen (depending on your timer interval). If you have timer interval set to 1000 milliseconds, then you can increase sum on each tick, but set label2.Text = (sum % 5).ToString().
every time your stopwatch TICKS, sum is inside TICK and it will reset and start from
int sum=0;
so try to make sum variable GLOBAL outside timer1_Tick event and it will continue increasing.
You will have to move sum out of the timer callback as you are setting it to 0 each time the timer elapses
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int sum = 0;
private DateTime lastUpdate;
private Stopwatch sw = new Stopwatch();
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = string.Format("{0:00}:{1:00}:{2:00}",
sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds);
if (DateTime.Now >= lastUpdate.AddSeconds(5))
{
sum++;
lastUpdate = DateTime.Now;
label2.Text = sum.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
{
sw.Stop();
button1.Text = "stop";
}
else
{
sum = 0;
lastUpdate = DateTime.Now;
timer1.Enabled = true;
sw.Start();
button1.Text = "Start";
}
}
I have a timer and in 30 minutes I want to count clicks and show it in a textbox. but how? here is timer code:
decimal sure = 10;
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
sure--;
label3.Text = sure.ToString();
if (sure == 0)
{
timer1.Stop();
MessageBox.Show("Süre doldu");
}
}
Declare your clickCounter at global, and raise your counter++ in Mouse Click Event.
If you do it more specific, you can use Background Worker, to track time.
and use Application.DoEvents() to write remaining to to textBox
Put a button, 2 labels, and a timer. rename labels with lblClickCount and lblRemainingTime
private int clickCounter = 0;
private void button1_Click(object sender, EventArgs e)
{
clickCounter++;
lblClickCount.Text = clickCounter.ToString();
}
decimal sure = 10;
private void timer1_Tick(object sender, EventArgs e)
{
sure--;
lblRemainingTime.Text = sure.ToString();
Application.DoEvents();
if (sure == 0)
{
timer1.Stop();
MessageBox.Show("Süre doldu. Toplam tiklama sayisi:" + clickCounter.ToString());
}
}
If you wanted to reuse buttoN1 to count the clicks but not Start new timer you can add a if around the code you want to protect.
bool hasTimerStarted = false;
int numberOfClicks = 0;
private void button1_Click(object sender, EventArgs e)
{
if(!hasTimerStarted)
{
button1.Enabled = true;
timer1.Start();
hasTimerStarted = true;
}
++numberOfClicks;
}
When the timer expires you reset the count and if the timer has started.
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
label3.Text = elapsedTime;
labelClicks.Text = "User clicked " + clicksNo.toString() + "nt times..";
if (stopWatch.ElapsedMilliseconds >= this.minutes * 60 * 1000)
{
timer1.Stop();
MessageBox.Show("Time elapsed.");
hasTimerStarted = false;
numberOfClicks = 0;
}
}