How to insert a 2 second interval after the tone is played? - c#

System.Timers.Timer intervalTimer = new System.Timers.Timer(2000); // set the timer for 2 sec.
intervalTimer.Elapsed += new ElapsedEventHandler(intervalTimer_Elapsed);
private void doHearingTest(ArrayList TestSeq)
{
if (!initialTestCompleted && testPerFreqLeft == totalTestPerFreqForInit)
{
label1.Text += testFreqSet[(int)initTestSeq[currentFreqIndex]] + " , ";
if (currentFreqIndex == numOfFreqTested - 1)
label1.Text += " || ";
}
else if (initialTestCompleted && testPerFreqLeft == totalTestPerFreqForMalig)
{
label3.Text += testFreqSet[(int)maligTestSeq[currentFreqIndex]] + " , ";
if (currentFreqIndex == numOfFreqTested - 1)
label3.Text += " || ";
}
#endregion
leftRightPan = (currentFreqIndex < numOfFreqTested) ? 10000 : -10000; //right = 10000
int ear = (leftRightPan == 10000) ? 0 : 1;
int freqIndex = (int)TestSeq[currentFreqIndex];
int testVol = earFreqDBVolAdj[ear, freqIndex, currentTestVolIndex];
noResponseTimer.Start();
intervalTimer.Start();
playTone(testFreqSet[freqIndex], testVol, leftRightPan, tonePlayTime);
responseDetected = false;
respondCounter = 0;
testPerFreqLeft--;
}
private void intervalTimer_Elapsed(object sender, ElapsedEventArgs e)
{
intervalTimer.Stop();
}
I would like to have an interval of 2 seconds between the previous tone and next tone play. Any idea how this could this be done? Many thanks.

Seems to me you only need to call DoHearingTest() from your handler function and that should do the trick

You can check this link-
Example sets up an event handler for the Timer.Elapsed event, creates a timer, uses the Interval property to set the timer interval, and starts the timer. The event handler displays the SignalTime property each time it is raised.
http://msdn.microsoft.com/en-us/library/system.timers.timer.interval(v=vs.110).aspx

Use Windows.Form.Timer instead What you are using.
like;
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 2000; // time to occuer the tick event
t.Tick += new EventHandler(t_Tick); // create event
In you Tick Event after every Two minute Write you code for sound;
void t_Tick(object sender, EventArgs e)
{
t.stop();
doHearingTest(TestSeq); // this is your code to run at every two minute
t.start();
}
Please do the other changes as needed..!!

try
Thread.Sleep(2000);
which smakes the current thread sleep

Related

c# timer is going too fast if set

i'm trying to implement a simple countdown using Timer (using https://www.geoffstratton.com/cnet-countdown-timer code). it does work if i run the timer once but if i stop the timer or the timer goes to 00:00 the next time i'll start it, it will go 2x faster. if i stop it and start it again it will go 3x faster.
(my explaination may be not clear, i did a gif that demonstrate the problem)
https://media.giphy.com/media/fQr7sX6LNRECvQpCYP/giphy.gif
i'm very novice at c#, i usually figure things out but i cant get what's happening here.
I included the timer code. if somebody can help me with this it would be awesome!
Thanks !!!
private void btnStartTimer_Click(object sender, EventArgs e)
{
if (txtTimer.Text == "00:00")
{
MessageBox.Show("Please enter the time to start!", "Enter the Time", MessageBoxButtons.OK);
}
else
{
string[] totalSeconds = txtTimer.Text.Split(':');
int minutes = Convert.ToInt32(totalSeconds[0]);
int seconds = Convert.ToInt32(totalSeconds[1]);
timeLeft = (minutes * 60) + seconds;
btnStartTimer.Enabled = false;
btnCleartimer.Enabled = false;
txtTimer.ReadOnly = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
}
private void btnStopTimer_Click(object sender, EventArgs e)
{
timer1.Stop();
timeLeft = 0;
btnStartTimer.Enabled = true;
btnCleartimer.Enabled = true;
txtTimer.ReadOnly = false;
}
private void btnCleartimer_Click(object sender, EventArgs e)
{
txtTimer.Text = "00:00";
}
private void timer1_Tick(object sender, EventArgs e)
{
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
// Display time remaining as mm:ss
var timespan = TimeSpan.FromSeconds(timeLeft);
txtTimer.Text = timespan.ToString(#"mm\:ss");
// Alternate method
//int secondsLeft = timeLeft % 60;
//int minutesLeft = timeLeft / 60;
}
else
{
timer1.Stop();
SystemSounds.Exclamation.Play();
MessageBox.Show("Time's up!", "Time has elapsed", MessageBoxButtons.OK);
}
}
You need to unsubscribe from the event in your btnStopTimer_Click method:
timer1.Tick -= timer1_Tick;
You are adding the event to Count every time you start the timer. As a result, the first time you call it there is only one event, the second time two events and so on. As a result, you first go down one second, then two,....
I would recommend creating the timer separately and just call Start and Stop.
Alternativ, user Dmitry Korolev answered a good Approach if you don't want to create the timer somewhere else
timer1.Tick -= timer1_Tick;

How I can stop a timer with seconds in real time c#?

I'm trying to stop a timer when 16 seconds in real time have passed, but i don't know how i can do that.
I made this little example: when picturebox1 intersects with picturebox2,this action activate a timer, and this timer have to shows the picturebox3 during 16 seconds in real time and after stop it(timer) (and the picturebox3 doesn't show).
(Sorry for my english. But StackOverflow in Spanish doesn't have many information).
I'm using windows form and C#
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox7.Hide();
if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible) || (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
{
puntaje++;
this.Text = "Puntaje: " + puntaje;
if (puntaje % 5 == 0)
{
timer3.Enabled=true;
//This is the part where i want set down the timer3, timer 2 is on
}
}
You can try this, on your timer tick event handler. Timespan counts the elapsed time between two dates. On this case since its 16 seconds, we count it by negative.
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = dtStart.Subtract(DateTime.Now);
if (ts.TotalSeconds <= -16)
{
timer1.Stop();
}
}
Make sure your dtStart (DateTime) is declared when you start your timer:
timer1.Start();
dtStart = DateTime.Now;
The cleanest way I can see this implemented is by using the interval parameter of a System.Timers.Timer.
Here's a sample snippet of the code
var timer = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
timer.Elapsed += (sender, e) =>
{
Console.WriteLine($"Finished at exactly {timer.Interval} milliseconds");
};
_timer.Start();
The TimeSpan.FromSeconds(16).TotalMilliseconds basically converts to 16000 but I used the TimeSpan static method for you to understand it easier and looks more readable.
The AutoReset property of the timer tells it that it should only be triggered once.
Adjusted for your code
private void timer2_Tick(object sender, EventArgs e)
{
pictureBox7.Hide();
if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible)
|| (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
{
puntaje++;
this.Text = "Puntaje: " + puntaje;
if (puntaje % 5 == 0)
{
var timer3 = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
timer3.Elapsed += (sender, e) =>
{
pictureBox3.Visible = true;
};
timer3.Start();
}
}
}
Please do mark the question Answered if this solves your issue.

Check if the countdown timer is 0

I have a script based on the countdown timer. I want that when the time reaches 0, the timer stop and a message appear. The code id this:
public partial class simulare : Form
{
private admin admin;
Timer timer = new Timer();
public simulare(admin admin)
{
InitializeComponent();
this.admin=admin;
label2.Text = TimeSpan.FromMinutes(0.1).ToString();
}
private void simulare_Load(object sender, EventArgs e)
{
var startTime = DateTime.Now;
timer = new Timer() { Interval = 1000 };
timer.Tick += (obj, args) =>
label2.Text = (TimeSpan.FromMinutes(0.1) - (DateTime.Now - startTime)).ToString("hh\\:mm\\:ss");
timer.Enabled = true;
timer.Start();
if (condition)
{
timer.Stop();
MessageBox.Show("Done!");
}
}
}
I tried those conditions, but unsuccessful:
if (timer.ToString() == TimeSpan.Zero.ToString())
if (label2.Text.ToString() == TimeSpan.Zero.ToString())
if (label2.Text == TimeSpan.Zero)
You could extract the calculation and assign the result to a TimeSpan variable, then check if the Seconds in that TimeSpan variable are equals to zero
void simulare_Load(object sender, EventArgs e)
{
var startTime = DateTime.Now;
timer = new System.Windows.Forms.Timer() { Interval = 1000 };
timer.Tick += (obj, args) =>
{
TimeSpan ts = TimeSpan.FromMinutes(0.1) - (DateTime.Now - startTime);
label1.Text = ts.ToString("hh\\:mm\\:ss");
if (ts.Seconds == 0)
{
timer.Stop();
MessageBox.Show("Done!");
}
};
timer.Start();
}
First off, checking anything in the Load event isn't going to work. That code only runs once (on form load).
So you need a more complex tick event, which I would put into an actual function instead of a lambda:
private int countDown = 50; //Or initialize at load time, or whatever
public void TimerTick(...)
{
label2.Text = (TimeSpan.FromMinutes(0.1) - (DateTime.Now - startTime)).ToString("hh\\:mm\\:ss");
countDown--;
if (countDown <= 0)
timer.Stop();
}
I use an int counter here since checking against a view property (the text in this case) isn't a very good design/practice. If you really want a TimeSpan, I would still save it off instead of checking directly against the Text property or a string.

C# timer stop after some number of ticks automatically

How to stop a timer after some numbers of ticks or after, let's say, 3-4 seconds?
So I start a timer and I want after 10 ticks or after 2-3 seconds to stop automatically.
Thanks!
You can keep a counter like
int counter = 0;
then in every tick you increment it. After your limit you can stop timer then. Do this in your tick event
counter++;
if(counter ==10) //or whatever your limit is
yourtimer.Stop();
When the timer's specified interval is reached (after 3 seconds), timer1_Tick() event handler will be called and you could stop the timer within the event handler.
Timer timer1 = new Timer();
timer1.Interval = 3000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(timer1_Tick);
void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop(); // or timer1.Enabled = false;
}
i generally talking because you didn't mention which timer, but they all have ticks... so:
you'll need a counter in the class like
int count;
which you'll initialize in the start of your timer, and you'll need a dateTime like
DateTime start;
which you'll initialize in the start of your timer:
start = DateTime.Now;
and in your tick method you'll do:
if(count++ == 10 || (DateTime.Now - start).TotalSeconds > 2)
timer.stop()
here is a full example
public partial class meClass : Form
{
private System.Windows.Forms.Timer t;
private int count;
private DateTime start;
public meClass()
{
t = new Timer();
t.Interval = 50;
t.Tick += new EventHandler(t_Tick);
count = 0;
start = DateTime.Now;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
if (count++ >= 10 || (DateTime.Now - start).TotalSeconds > 10)
{
t.Stop();
}
// do your stuff
}
}
Assuming you are using the System.Windows.Forms.Tick. You can keep track of a counter, and the time it lives like so. Its a nice way to use the Tag property of a timer.
This makes it reusable for other timers and keeps your code generic, instead of using a globally defined int counter for each timer.
this code is quiet generic as you can assign this event handler to manage the time it lives, and another event handler to handle the specific actions the timer was created for.
System.Windows.Forms.Timer ExampleTimer = new System.Windows.Forms.Timer();
ExampleTimer.Tag = new CustomTimerStruct
{
Counter = 0,
StartDateTime = DateTime.Now,
MaximumSecondsToLive = 10,
MaximumTicksToLive = 4
};
//Note the order of assigning the handlers. As this is the order they are executed.
ExampleTimer.Tick += Generic_Tick;
ExampleTimer.Tick += Work_Tick;
ExampleTimer.Interval = 1;
ExampleTimer.Start();
public struct CustomTimerStruct
{
public uint Counter;
public DateTime StartDateTime;
public uint MaximumSecondsToLive;
public uint MaximumTicksToLive;
}
void Generic_Tick(object sender, EventArgs e)
{
System.Windows.Forms.Timer thisTimer = sender as System.Windows.Forms.Timer;
CustomTimerStruct TimerInfo = (CustomTimerStruct)thisTimer.Tag;
TimerInfo.Counter++;
//Stop the timer based on its number of ticks
if (TimerInfo.Counter > TimerInfo.MaximumTicksToLive) thisTimer.Stop();
//Stops the timer based on the time its alive
if (DateTime.Now.Subtract(TimerInfo.StartDateTime).TotalSeconds > TimerInfo.MaximumSecondsToLive) thisTimer.Stop();
}
void Work_Tick(object sender, EventArgs e)
{
//Do work specifically for this timer
}
When initializing your timer set a tag value to 0 (zero).
tmrAutoStop.Tag = 0;
Then, with every tick add one...
tmrAutoStop.Tag = int.Parse(tmrAutoStop.Tag.ToString()) + 1;
and check if it reached your desired number:
if (int.Parse(tmrAutoStop.Tag.ToString()) >= 10)
{
//do timer cleanup
}
Use this same technique to alternate the timer associated event:
if (int.Parse(tmrAutoStop.Tag.ToString()) % 2 == 0)
{
//do something...
}
else
{
//do something else...
}
To check elapsed time (in seconds):
int m = int.Parse(tmrAutoStop.Tag.ToString()) * (1000 / tmrAutoStop.Interval);

Display a timer that loops every 15 seconds

I made this code, but there is a delay between the time loop showing on the screen and the exact elapsed time.
Timer t = new Timer();
int time = 15;
string timestr;
t.Interval = 1000;
t.Tick += new EventHandler(Time);
void Time(object sender, EventArgs e)
{
if (time == 0)
{ time = 15; }
if (time != 0)
{
time--;
timestr = time.ToString();
label.Text = timestr;
}
}
My guess is that you are off by one second since the timer won't fire its first event until that interval value is reached.
A quick fix would be to fire the event yourself when you start it:
t.Start();
Time(t, EventArgs.Empty);
I think you need to try this. Add the line Application.DoEvents() just before the end of Time function.
void Time(object sender, EventArgs e)
{
if (time == 0)
{ time = 15; }
if (time != 0)
{
time--;
timestr = time.ToString();
label.Text = timestr;
}
Application.DoEvents();
}

Categories

Resources