How can I implement a countdown timer in my Windows Phone 8.1 app? There seems to be no information available for it. All I could find works for either a Windows Forms application or a Windows Application, but none of them seems to be working on the phone app.This is what I am doing-
namespace Timer
{
public partial class MainPage : Page
{
DispatcherTimer mytimer = new DispatcherTimer();
int currentcount = 0;
public MainPage()
{
InitializeComponent();
mytimer = new DispatcherTimer();
mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
mytimer.Tick += new EventHandler(mytime_Tick);
//HERE error comes Cannot implicitly convert type System.EventHandler to System.EventHandler<object>
}
private void mytime_Tick(object sender,EventArgs e)
{
timedisplayBlock.Text = currentcount++.ToString();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
mytimer.Start();
}
}
}
But it gives me this error-
Cannot implicitly convert type System.EventHandler to System.EventHandler<object>
Should your increment not be written as
timedisplayBlock.Text = (++currentcount).ToString();
Or
currentcount++;
timedisplayBlock.Text = currentcount.ToString();
I don't think the increment will matter too much but still should be written correctly to ensure you do not leave yourself a count behind.
- See Sergiol's issue with David's answer on the link below https://stackoverflow.com/a/7848129/2110465
The other thing i have noticed is that you are initializing the DispatcherTimer twice...
DispatcherTimer mytimer = new DispatcherTimer();
...
..
mytimer = new DispatcherTimer();
It is better practice to initialize upon the instance to save overhead, though depending on the scope of use. Given i am not aware of how the rest of your code uses it, i suggest it be rewritten as follows
namespace Timer
{
public partial class MainPage : Page
{
DispatcherTimer mytimer = new DispatcherTimer();
int currentcount = 0;
public MainPage()
{
InitializeComponent();
mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
mytimer.Tick += new EventHandler(mytime_Tick);
}
private void mytime_Tick(object sender, EventArgs e)
{
timedisplayBlock.Text = (++currentcount).ToString();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
mytimer.Start();
}
}
}
Saying all of the above, i could not reproduce the fault when replicated...
EDIT - This may be your issue (https://stackoverflow.com/a/16636862/2110465)
You misspelled the event handler name the event handler definition needs to be changing too, it should be:
mytimer.Tick += mytime_Tick; // removed the 'r'
Change
private void mytimer_Tick(object sender, EventArgs e)
to
private void mytimer_Tick(object sender, object e).
Related
I have one button name called Submit. I would like to trigger Auto Button_Click event for every 5sec.
E.g:
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to WPF....");
}
Every 5sec I need to call this Button_Click event to show Message like "Welcome to Google...." automatically.
Please help me to solve.
In Wpf, you could use DispatcherTimer
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Timer
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += (s, ev) => btnClickMe.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
timer.Interval = new TimeSpan(0, 5, 0);
timer.Start();
}
create a timer to run every 5 seconds and send: Button_Click(null, null);
public static void Main()
{
var timer = new Timer();
timer.Elapsed+= OnTimedEvent;
timer.Interval=5000;
timer.Enabled=true;
Console.ReadKey();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Button_Click(null, null);
}
It is as simple as this. Create a timer of 5 seconds duration and do this on the timer_tick event.
buttonName.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
I have a dll that has a timer control in it, inside I have a message box. The timer has been enabled and the interval has been set to 100 seconds, but for some reason it's not firing. I added button to check if it's enabled, and timer1.enabled property is set to true, but it doesn't fire even once. Any ideas what could be wrong? Thanks!
Dll Code:
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
This is how I call the dll form:
M.ModuleInterface module = Activator.CreateInstance(t) as M.ModuleInterface;
Thread t = new Thread(module.showForm);
t.Start();
showForm Method:
void M.ModuleInterface.showForm()
{
log("GUI::Initialized()");
frm.ShowDialog();
}
i believe, judging by your words alone, that you simply forgot to register to the time.
do:
public Form1()
{
InitializeComponent();
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
// Your code here
}
this little example works just fine:
private System.Windows.Forms.Timer timer1;
public Form1()
{
InitializeComponent();
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 100;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
// timer is triggered. code here is called
}
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();
}
the code below works in Windows Phone 7
private void ShowTime()
{
txtTime.Text = get24hour();
//display the Date and week.
DateTime nowtime = DateTime.Now;
txtWeek.Text = nowtime.DayOfWeek.ToString();
txtDate.Text = nowtime.Date.ToString("MM/dd");
//create timer to fresh to time
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMinutes(1);
timer.Tick += timer_Ticker;
timer.Start();
}
private void timer_Ticker(object sender, EventArgs e)
{
txtTime.Text = get24hour();
}
private string get24hour()
{
return DateTime.Now.ToString("HH:mm");
}
but error in WinRT (Metro)
error part:
timer.Tick += timer_Ticker;
error message:
No overload for 'timer_Ticker' matches delegate 'System.EventHandler<object>'
what I do
I try to change the code to
private void timer_Ticker()
{
txtTime.Text = get24hour();
}
result
but it is not work again, why and how to solve it? :(
timer.Tick += new EventHandler<object>(timer_Tick);
private void timer_Tick(object sender, object e)
{
}
Refer to this link
I read the msdn and change the delegate method to below and it works:
private void timer_Ticker(object sender, object e)
{
txtTime.Text = get24hour();
}
Hi I am working with Windows.Forms.Timer with Web Application . I create Timer.Tick event handler to handle Timer_Tick but I am not successfull. I don't get any error but I can not get result even. Here is my code
System.Windows.Forms.Timer StopWatchTimer = new System.Windows.Forms.Timer();
Stopwatch sw = new Stopwatch();
public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Enabled = true;
StopWatchTimer.Interval = 1;
StopWatchTimer.Start();
this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick);
sw.Start();
}
protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e)
{
StopWatchTimer.Stop();
sw.Reset();
StopWatchLbl.Text = "00:00:00:000";
}
public void StopWatchTimer1_Tick(object sender,EventArgs e)
{
TimeSpan elapsed = sw.Elapsed;
StopWatchLbl.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}",
Math.Floor(elapsed.TotalHours),
elapsed.Minutes,
elapsed.Seconds,
elapsed.Milliseconds);
}
From the MSDN documentation for Windows Forms Timer (emphasis mine):
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.
This timer will not work in a web application. You'll need to use another class, like System.Timers.Timer. This has it's own pitfalls, however.
Did you try defining the Tick event prior to starting the timer?
this.StopWatchTimer.Tick += new EventHandler(StopWatchTimer1_Tick);
StopWatchTimer.Start();
public partial class TestFrom : Form
{
private Thread threadP;
private System.Windows.Forms.Timer Timer = new System.Windows.Forms.Timer();
private string str;
public TestFrom()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Timer.Interval =100;
Timer.Tick += new EventHandler(TimeBussiness);
Timer.Enabled = true;
Timer.Start();
Timer.Tag = "Start";
}
void TimeBussiness(object sender, EventArgs e)
{
if (threadP.ThreadState == ThreadState.Running)
{
Timer.Stop();
Timer.Tag = "Stop";
}
else
{
//do my bussiness1;
}
}
private void button3_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(Salver);
threadP= new Thread(threadStart);
threadP.Start();
}
private void Salver()
{
while (Timer.Tag == "Stop")
{
}
//do my bussiness2;
Timer.Start();
Timer.Tag = "Start";
}
}