Solved

Timer

Posted on 2006-06-17
4
364 Views
Last Modified: 2010-04-23
Why timer is not working?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        tmr.Start()


    End Sub

    Private Sub GetCount()


        Me.Label4.Text = Directory.GetFiles("C:\Documents and Settings\Sample\Desktop\CVS", "MC0201*.CSV").Length
        Me.Label1.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "MC0012*.CSV").Length
        Me.Label3.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "*.CSV").Length
        Me.Label2.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "*.XLS").Length





    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        tmr = New System.Timers.Timer
        tmr.Interval = 5000


    End Sub

    Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed


        GetCount()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        tmr.Stop()

    End Sub
0
Comment
Question by:VBdotnet2005
  • 2
4 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16928111
Hi VBdotnet2005;

That code should work. Are you getting errors or something else is leading you to think it is not working?

Fernando
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 16928126
I agree...it should work.

The System.Timers.Timer runs in its own thread though so you are incorrectly updating GUI elements from a different thread.  Either change to System.Windows.Forms.Timer (which has a Tick event not Elapsed), or use Delegates and Invoke.

Read up here:
http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/

    "This means that the code inside your Elapsed event handler must conform to a golden rule of Win32 programming: an instance of a control should never be accessed from any thread other than the thread that was used to instantiate it."
0
 

Author Comment

by:VBdotnet2005
ID: 16928577
Error

"Cross-thread operation not valid: Control 'Label4' accessed from a thread other than the thread it was created on."


This line  

        Me.Label4.Text = Directory.GetFiles("C:\Documents and Settings\Sample\Desktop\CVS", "MC0201*.CSV").Length
0
 

Author Comment

by:VBdotnet2005
ID: 16928583
I changed it to  System.Windows.Forms.Timer    
It works :) Thanks again Idle_Mind


Imports System.IO

Public Class Form1

    Private WithEvents tmr As Windows.Forms.Timer

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        tmr.Start()


    End Sub

Private Sub GetCount()


         Me.Label4.Text = Directory.GetFiles("C:\Documents and Settings\Sample\Desktop\CVS", "MC0201*.CSV").Length
        Me.Label1.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "MC0012*.CSV").Length
        Me.Label3.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "*.CSV").Length
        Me.Label2.Text = Directory.GetFiles("c:\documents and settings\Sample\desktop\cvs", "*.XLS").Length





End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        tmr = New Windows.Forms.Timer

        tmr.Interval = 5000


    End Sub

 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        tmr.Stop()

    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick

        GetCount()

    End Sub


End Class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now