Hi everyone,
I recently started working with wpf and am still a noob, but I am trying to create a timer program that starts and stops and sends the value to a label. I cannot seem to get the label text to update. I have done some research and found that I need to use the delegate or something like that. I do no know how and would appreciate any help you could give me.
Thanks,
Hersh K. Bhargava
Timer ProgramWPF IS STARTING TO ANNOY ME!
Page 1 of 1
11 Replies - 1682 Views - Last Post: 02 November 2010 - 04:45 AM
Replies To: Timer Program
#2
Re: Timer Program
Posted 31 October 2010 - 10:18 AM
Show us what you got so far and then we can better assess the problem.
#3
Re: Timer Program
Posted 31 October 2010 - 10:23 AM
Amrykid, on 31 October 2010 - 09:18 AM, said:
Show us what you got so far and then we can better assess the problem.
Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Windows.Threading
Imports System.Timers
Partial Public Class ViolinLog
Public time As Integer
Friend WithEvents myTimer As New Timer
Public Delegate Sub nextprimedelegate()
Public Sub New()
MyBase.New()
Me.InitializeComponent()
' Insert code required on object creation below this point.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
myTimer.Interval = 1000
myTimer.Start()
End Sub
Private Sub mytimer_tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myTimer.Elapsed
time = time + 1
End Sub
Private Sub refreshLabel(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Content = time
Label1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle, New NextPrimeDelegate(AddressOf Me.refreshlabel))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
myTimer.Stop()
End Sub
End Class
The main part that I do not understand is the delegate. What are they and how do they work?
Thanks,
Hersh
This post has been edited by hbhargava: 31 October 2010 - 10:24 AM
#4
Re: Timer Program
Posted 31 October 2010 - 02:41 PM
Use the DispatcherTimer and you won't have to worry about the delegate.
#5
Re: Timer Program
Posted 31 October 2010 - 05:01 PM
eclipsed4utoo, on 31 October 2010 - 01:41 PM, said:
Use the DispatcherTimer and you won't have to worry about the delegate.
Thanks, but what exactly is the difference and how do I implement it?
#6
Re: Timer Program
Posted 01 November 2010 - 05:55 AM
The difference is that the System.Timers.Timer(the one that you are using) runs on a different thread than the UI. To access the UI, you have to invoke the action to the Dispatcher. If you use the DispatcherTimer, you don't have to do that since it's already running on the same thread as the Dispatcher.
The DispatcherTimer is fine to use when you want to run a small, quick amount of code at a desired interval. The code that you want to run will execute and finish quickly. Therefore, it's not a problem to use the DispatcherTimer in this instance. You would NOT want to use the DispatcherTimer for long running processes.
To implement it, you could use this code...
Basically, the DispatcherTimer is just the same as the System.Windows.Forms.Timer in WinForms. Tick event occurs on the UI thread so there's no need to do any invokes.
The DispatcherTimer is fine to use when you want to run a small, quick amount of code at a desired interval. The code that you want to run will execute and finish quickly. Therefore, it's not a problem to use the DispatcherTimer in this instance. You would NOT want to use the DispatcherTimer for long running processes.
To implement it, you could use this code...
public partial class MainWindow : Window
{
DispatcherTimer timer;
public MainWindow()
{
InitializeComponent();
// creates a new timer
timer = new DispatcherTimer();
// sets it's interval to 1 second
timer.Interval = TimeSpan.FromSeconds(1);
// this creates an anonymous method for the Tick event
// you can do this instead of creating a new Event
timer.Tick += delegate(object s, EventArgs e)
{
// this code will run when the Tick event has fired.
int number = 0;
int.TryParse(Label1.Content.ToString(), out number);
number++;
Label1.Content = number;
};
}
private void button1_Click(object sender, RoutedEventArgs e)
{
timer.Start();
}
}
Basically, the DispatcherTimer is just the same as the System.Windows.Forms.Timer in WinForms. Tick event occurs on the UI thread so there's no need to do any invokes.
#7
Re: Timer Program
Posted 01 November 2010 - 08:19 AM
I am coding this in VB.NET and I am having a bit of trouble with your delegate. There is no timer.tick event.
Help would be greatly appreciated.
Thanks,
Hersh
Help would be greatly appreciated.
Thanks,
Hersh
#8
Re: Timer Program
Posted 01 November 2010 - 09:15 AM
I used Telerik's code converter to do this...
See if that works for you.
Public Partial Class MainWindow Inherits Window Private timer As DispatcherTimer Public Sub New() InitializeComponent() ' creates a new timer timer = New DispatcherTimer() ' sets it's interval to 1 second timer.Interval = TimeSpan.FromSeconds(1) ' this creates an anonymous method for the Tick event ' you can do this instead of creating a new Event timer.Tick += Function(s As Object, e As EventArgs) Do ' this code will run when the Tick event has fired. Dim number As Integer = 0 Integer.TryParse(Label1.Content.ToString(), number) number += 1 Label1.Content = number End Function End Sub Private Sub button1_Click(sender As Object, e As RoutedEventArgs) timer.Start() End Sub End Class
See if that works for you.
#9
Re: Timer Program
Posted 01 November 2010 - 12:09 PM
eclipsed4utoo, on 01 November 2010 - 08:15 AM, said:
I used Telerik's code converter to do this...
See if that works for you.
Public Partial Class MainWindow Inherits Window Private timer As DispatcherTimer Public Sub New() InitializeComponent() ' creates a new timer timer = New DispatcherTimer() ' sets it's interval to 1 second timer.Interval = TimeSpan.FromSeconds(1) ' this creates an anonymous method for the Tick event ' you can do this instead of creating a new Event timer.Tick += Function(s As Object, e As EventArgs) Do ' this code will run when the Tick event has fired. Dim number As Integer = 0 Integer.TryParse(Label1.Content.ToString(), number) number += 1 Label1.Content = number End Function End Sub Private Sub button1_Click(sender As Object, e As RoutedEventArgs) timer.Start() End Sub End Class
See if that works for you.
Timer.tick is still not recognized. Any ideas?
#11
Re: Timer Program
Posted 01 November 2010 - 01:48 PM
#12
Re: Timer Program
Posted 02 November 2010 - 04:45 AM
Can you show all of your code?
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|