Timer Program

WPF IS STARTING TO ANNOY ME!

Page 1 of 1

11 Replies - 1895 Views - Last Post: 02 November 2010 - 04:45 AM Rate Topic: -----

#1 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

Timer Program

Posted 31 October 2010 - 08:56 AM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Timer Program

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

Re: Timer Program

Posted 31 October 2010 - 10:23 AM

View PostAmrykid, 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

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Timer Program

Posted 31 October 2010 - 02:41 PM

Use the DispatcherTimer and you won't have to worry about the delegate.
Was This Post Helpful? 0
  • +
  • -

#5 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

Re: Timer Program

Posted 31 October 2010 - 05:01 PM

View Posteclipsed4utoo, 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?
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

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...

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.
Was This Post Helpful? 2
  • +
  • -

#7 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

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
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Timer Program

Posted 01 November 2010 - 09:15 AM

I used Telerik's code converter to do this...

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.
Was This Post Helpful? 0
  • +
  • -

#9 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

Re: Timer Program

Posted 01 November 2010 - 12:09 PM

View Posteclipsed4utoo, on 01 November 2010 - 08:15 AM, said:

I used Telerik's code converter to do this...

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?
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Timer Program

Posted 01 November 2010 - 12:45 PM

Can you post your code?
Was This Post Helpful? 0
  • +
  • -

#11 hbhargava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 07-August 09

Re: Timer Program

Posted 01 November 2010 - 01:48 PM

View Posteclipsed4utoo, on 01 November 2010 - 11:45 AM, said:

Can you post your code?

 timer.Tick += Function(s As Object, e As EventArgs)
                          'stuff here
                      End Function

Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Timer Program

Posted 02 November 2010 - 04:45 AM

Can you show all of your code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1