I have some MediaElements in my WPF-application
If i call Question1_5.Play(); then Timer_activated.Play(); does not play on time (this audio plays 5-6 seconds after calling Play() method).
What the problem?
private void NewGame_MouseLeftButtonDown(object sender,
MouseButtonEventArgs e) {
Dispatcher.BeginInvoke((Action)(() => Windows.SelectedItem = Game));
Questions1_5.Play();
sb = FindResource("TimerAppear") as Storyboard;
sb.Begin();
sb.Pause();
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 4);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
Timer_activated.Play();
sb.Resume();
sb = FindResource("Seconds15") as Storyboard;
timer.Stop();
}
Related
I want to change the interval of my dispatcher time in run time
here is my code :
InitializeComponent();
DispatcherTimer messageTimer = new DispatcherTimer();
messageTimer.Tick += new EventHandler(messageTimer_Tick);
messageTimer.Interval = TimeSpan.FromSeconds(1);
messageTimer.Start();
idk how to change the interval of the DispatcherTimer at run time
I created two textblocks and a button.
textblock : x:Name="txt_Count"
textblock : x:Name="txt_TimeNow"
Button : x:Name="btn_changeTime_s"
int count = 0;
DispatcherTimer messageTimer;
DispatcherTimer TimeNow;
public MainWindow()
{
InitializeComponent();
messageTimer = new DispatcherTimer();
messageTimer.Tick += new EventHandler(messageTimer_Tick);
messageTimer.Interval = TimeSpan.FromSeconds(1);
messageTimer.Start();
TimeNow = new DispatcherTimer();
TimeNow.Tick += new EventHandler(TimeNow_Tick);
TimeNow.Interval = TimeSpan.FromMilliseconds(100);
TimeNow.Start();
}
private void TimeNow_Tick(object sender, EventArgs e)
{
txt_now.Text = DateTime.Now.ToString();
}
private void messageTimer_Tick(object sender, EventArgs e)
{
txt_Count.Text = count.ToString();
count++;
}
private void btn_changeTime_s_Click(object sender, RoutedEventArgs e)
{
messageTimer.Interval = TimeSpan.FromSeconds(2);
}
If you need to modify the Main UI Thread in the background, use the syntax
txt_Count.Dispatcher.Invoke(DispatcherPriority.Normal,new Action(delegate ()
{
txt_Count.Text = count.ToString();
}
));
I have a code like this in windows phone
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
At first use, the display right from 1,2,3,4 ... but when you click again, it starts from 2, 4, 6 .... click again, the display 3, 6 ...
Is there a way for it to run your display when clicking the button 1,2,3,4 each failure?
thank!!!
Try something like this:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
if (timer2 != null)
{
timer2 -= timer_Tick2; // unassigns the event handler
timer2.Stop(); // stops the timer
}
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
EDIT: If you just want to start the timer once, it could be reduced to:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
if (timer2 == null)
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
Use Stop() method to stop the timer every click before run it again.
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
timer2.Stop();
Start_timer();
}
Each timer registers on the same Tick callback:
timer2.Tick += timer_Tick2;
so it's the reason of the problem.
I am trying to make a program with 2 timers in it running at different intervals. Currently I have 1 timer working fine and I need to have another one running. My code for the first timer that works looks like this:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
**code for timer in here
}
This works fine, but now I need another timer running at a 1second interval with different code and when I try to duplicate this by just making all the dispatcherTimer into dispatcherTimer2 I am running into errors.
I am not sure what you were doing (you should post the error), but the following works with your supplied code:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
// New timer
System.Windows.Threading.DispatcherTimer dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer2.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//code for timer in here
}
private void dispatcherTimer2_Tick(object sender, EventArgs e)
{
//code for timer2 in here
}
How can I change the text of button with timeout? I tried out with the following code but it is not working.
private void button1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (button1.Text == "Start")
{
//do something
button1.Text = "stop"
if (sw.ElapsedMilliseconds > 5000)
{
button1.Text = "Start";
}
}
How can I correct my code?
You need to use Timer instead:
Timer t = new Timer(5000); // Set up the timer to trigger on 5 seconds
t.SynchronizingObject = this; // Set the timer event to run on the same thread as the current class, i.e. the UI
t.AutoReset = false; // Only execute the event once
t.Elapsed += new ElapsedEventHandler(t_Elapsed); // Add an event handler to the timer
t.Enabled = true; // Starts the timer
// Once 5 seconds has elapsed, your method will be called
void t_Elapsed(object sender, ElapsedEventArgs e)
{
// The Timer class automatically runs this on the UI thread
button1.Text = "Start";
}
Stopwatch is only for measuring how much time has passed since you called Start().
If you're using C# 5
private async void button1_Click(object sender, EventArgs e)
{
button1.Text = "Stop";
await Task.Delay(5000);
button1.Text = "Start";
}
You could use a timer. In this example the text of the button changes to "Stop" after 5 seconds.
private Timer timer = new Timer();
private void button1_Click(object sender, EventArgs e)
{
timer.Interval = 5000; // interval length
timer.Tick += TimerOnTick;
timer.Enabled = true; // activate timer
button1.Text = "Start";
}
private void TimerOnTick(object sender, EventArgs eventArgs)
{
timer.Enabled = false; // deactivate timer
button1.Text = "Stop";
}
I think you can reach your goal by using Timer
Example of using Timer
public partial class FormWithTimer : Form
{
Timer timer = new Timer();
public FormWithTimer()
{
InitializeComponent();
// Everytime timer ticks, timer_Tick will be called
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (1000) * (1); // Timer will tick every second
timer.Enabled = true; // Enable the timer
}
// .......
showForm() // declaration
{
timer.start();
// .......
timer.stop();
}
void timer_Tick(object sender, EventArgs e)
{
//hide form...through visibility
}
}
Use this instead of Stopwatch:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "stop"
aTimer = new System.Timers.Timer(5000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
button1.Text = "Start";
var atim = source as Timer;
if (atim != null)
atim.Elapsed -= OnTimedEvent;
}
what i want to do is pause a video after every 10s
the video should pause after ever 10s till the video ends
the code given below gives unexpected results
the video pauses fine for the firs time (i.e after 10s)
but when i play again it should pause after 10s but in my case it pauses randomly sometimes at 8s,3s 5s and etc
what should i do??
please help
thanks!!
void PlayClick(object sender, EventArgs e)
{
VideoControl.Play();
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
VideoControl.Pause();
}
Add this in your dispatcherTimer_Tick-Method:
dispatcherTimer.Stop();
Move the following part into the constructor:
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
Make the DispatcherTimer a global variable.
EDIT: Thats how it should look like:
class MyClass
{
private DispatcherTimer _dispatcherTimer; //now your dispatcherTimer is accessible everywhere in this class
public MyClass()
{
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
_dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
}
void PlayClick(object sender, EventArgs e)
{
VideoControl.Play();
_dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object Sender, EventArgs e)
{
_dispatcherTimer.Stop();
VideoControl.Pause();
}
}
Bring the declaration of the timer out into a private class variable, move a couple lines to the constructor of the class, and stop the timer in the Tick handler.
The reason you don't want to keep creating the timer is because there are unmanaged resources involved with a timer and so you're closing that loop.
private dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
ctor
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
}
void PlayClick(object sender, EventArgs e)
{
VideoControl.Play();
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
dispatchTimer.Stop();
VideoControl.Pause();
}
Try with following code in Tick event:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
(sender as DispatcherTimer).Stop();
VideoControl.Pause();
}
You can make dispatcherTimer object outside Playclick event and only put Start() method inside PlayClick event in following way:
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
public Form1() //// form constructor where you are handling these all event....
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
}
void PlayClick(object sender, EventArgs e)
{
VideoControl.Play();
dispatcherTimer .Start();
}