Timer Tick more than 1 time at once - c#

I am making a program where the timer1 should activate another timer2 and then stop, in timer2 i activate timer1 again and stop timer2 and so it goes on and then I have a text log where it write the progress down. And here is the problem, first its starts with 2 of Timer1 tick written out then 2 of timer 2 then it gets multiplied by 2 so its 4 next time then 8 then 16 so forth and I just want it to be 1 timer1 than 1 timer2 then its starts over again, I can't see what's wrong.
private void buttonStart_Click(object sender, EventArgs e)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = true;
timer1.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
buttonStart.Enabled = true;
buttonStop.Enabled = false;
timer1.Stop();
timer2.Stop();
}
private void LogWrite(string txt)
{
textBoxCombatLog.AppendText(txt + Environment.NewLine);
textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
}
private void timer1_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "player hit");
timer1.Stop();
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = true;
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
LogWrite(TimeDate + "mob hit");
timer2.Stop();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = true;
timer1.Start();
}

on timer1_tick you add event to timer2.tick event, so every time when timer1_tick function raises, you add one more event listener to timer2, but never remove old event handlers, the same situation with timer2_tick.
My advice to you is add these lines to your constructor and remove these lines from other functions:
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = true;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = true;
If you will do that, your timers will call always only once function per tick.

I'm sure this is what #Epsil0neR means...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = (1000);
timer1.Enabled = false;
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Interval = (1000);
timer2.Enabled = false;
}
private void buttonStart_Click(object sender, EventArgs e)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
timer1.Start();
}
private void buttonStop_Click(object sender, EventArgs e)
{
timer1.Stop();
timer2.Stop();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
}
private void LogWrite(string txt)
{
textBoxCombatLog.AppendText(txt + Environment.NewLine);
textBoxCombatLog.SelectionStart = textBoxCombatLog.Text.Length;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
LogWrite(TimeDate + "player hit");
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
LogWrite(TimeDate + "mob hit");
timer1.Start();
}
private string TimeDate
{
get { return DateTime.Now.ToString("HH:mm:ss") + ": "; }
}
}

Related

Timer Countdown with progress bar C#

I want to sync my progress bar in my project with the timer countdown.
This is what I has now:
namespace Timer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
int counter = 80;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.Increment(1);
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
}
}
I want the progress bar to finish when the timer to ends.
You need to specify the progressBar1 to have its .Step and Maximum properties match the Interval and counter variable. For example:
private int counter = 80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
// The max is the total number of iterations on the
// timer tick by the number interval.
progressBar1.Max = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
// Perform one step...
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}

User input for timer countdown

I would like to make my timer count down based on user input.
This is what my form looks like:
and this is my code:
private int counter=80;
DateTime dt = new DateTime();
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = counter * 1000;
progressBar1.Step = 1000;
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Start();
textBox1.Text = counter.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
progressBar1.PerformStep();
if (counter == 0)
{
timer1.Stop();
}
textBox1.Text = dt.AddSeconds(counter).ToString("mm:ss");
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Clear();
progressBar1.Value = 0;
}
How can I update the timer if the user enters a new value in the textbox?
Attach an OnTextChanged eventhandler to your textbox1. Stop the timer when textinput changes
protect void textinput1_OnTextChange(object sender, EventArg e) {
button2_Click(sender, e);
}
Or you can disable user input when timer starts and re-enable it once timer stopped.
Simply set textbox enabled value to false at start button clicked. And enable it again at stop of timer or checked on cancel button. Here is a code:
textBox1.Enabled = false;And
textBox1.Enabled = true;

Change control colour, and change it back to original

I want to change colour of the button momentarily to show that button has been pressed. How can I achieve this in C#? I can change the background color easily enough like this:
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Green;
}
of the button, but how do I revert those changes after a delay?
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Azure;
var aTimer = new System.Timers.Timer(2000);
aTimer.Elapsed += OnTimedEvent;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
button1.BackColor = SystemColors.Control;
}
Color btnBackColor;
Timer timer = new Timer();
private void button1_Click(object sender, EventArgs e)
{
btnBackColor = button1.backColor;
button1.BackColor = Color.Green;
timer.Enabled = true;
}
private void timer_Tick(object sender, EventArgs e)
{
button1.BackColor = btnBackColor;
timer.Enabled=false;
}
And add the following lines to the constructor:
timer.Tick += timer_Tick;
timer.Elapsed = 2000;

Change text every 30 percent in progress bar c#

i wanted to make every 30 percent in progress bar, the text changes.
What i wanted to do is, when the progress bar hit 25 percent, the text changes and it stop for a second, and it goes back to 50, and the text changes again, it keep going until it hit 100 percent.
Here is my code:
public WelcomeScreen()
{
InitializeComponent();
_timer.Interval = 2000;
label1.ForeColor = Color.White;
}
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 100;
progressBar1.Step = 25;
if (progressBar1.Step <= 25)
{
label1.Text = "Preparing Setup";
}
else if (progressBar1.Step <= 50)
{
label1.Text = "Preparing Application";
}
else if (progressBar1.Step <= 75)
{
label1.Text = "Preparing Database";
}
else if (progressBar1.Step <= 100)
{
label1.Text = "Preparing Contents";
}
else
{
label1.Text = "Launch Application";
}
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_timer.Enabled = true;
_timer.Tick += new EventHandler(Timer_Tick);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Preparing Setup");
currentWorker.ReportProgress(25, "Preparing Application");
currentWorker.ReportProgress(50, "Preparing Database");
currentWorker.ReportProgress(75, "Preparing Contents");
currentWorker.ReportProgress(100, "Launch Application");
}
void Timer_Tick(object sender, EventArgs e)
{
_timer.Enabled = false;
this.Hide();
_login.ShowDialog();
this.Close();
}
When the progress bar hit 100 percent, i order the application to wait until 2 seconds before show another form after the text changes to "Launch Application".
All the code in your WelcomeScreen_Load will block the UI thread until it is complete. This means that no matter what you do to your progress bar, it will never show it's changes until it is complete (which it will "jump" to the last settings).
You will want to look into Background Workers. These let you do your code async, and report back every-so-often with what the current state is (ie: the percent complete).
So, a really short example:
private void WelcomeScreen_Load(object sender, EventArgs e)
{
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var percentComplete = e.ProgressPercentage;
var userState = (string)e.UserState;
//do something with these values, like moving your progress bar
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = percentComplete;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// do something when the worker completes, like start your timer
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// do the "work" for the background worker
var currentWorker = (BackgroundWorker)sender;
currentWorker.ReportProgress(0, "Just Starting");
// do your first task
currentWorker.ReportProgress(25, "Finish First Task");
// ...
}

Timer function in c# ATM simulator

Hi i am trying to use the timer object in c# to update my database and set the card to confiscated if it is there for more than 5 seconds. Im having a little trouble. Will post my code below
private void timer1_Tick(object sender, EventArgs e)
{
if (seconds > 5)
{
timer1.Enabled = false;
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
}
private void Form1_load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public void cardConfiscated(string number)
{
atmCardsTableAdapter1.confiscated(number);
atmCardsTableAdapter1.FillByNotConfiscated(boG_10033009DataSet.ATMCards);
}
First thing's first, you're never setting the interval on your timer.
private void Form1_load(object sender, EventArgs e)
{
timer1 = new Timer(5000); // sets interval to 5 seconds
timer1.Elapsed += new new ElapsedEventHandler(timer1_Tick);
timer1.Start();
}
I'm doing a five second interval above, so that we don't have to call the timer more than once, when 5 seconds passes, we can just skip to the juicy stuff:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
Finally, you should note that if you're timer had a shorter interval, then you need to increment your seconds, for example:
private void Form1_load(object sender, EventArgs e)
{
timer1 = new Timer(1000); // sets interval to 1 second
timer1.Elapsed += new new ElapsedEventHandler(timer1_Tick);
timer1.AutoReset = true; // sets the timer to restart after each run
timer1.Start();
}
Then we'd need to increment seconds each interval, like you did.
private void timer1_Tick(object sender, EventArgs e)
{
seconds++;
if (seconds > 5)
{
timer1.Stop();
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
}

Categories

Resources