Solved

Timer

Posted on 2006-06-17
4
361 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

11 Experts available now in Live!

Get 1:1 Help Now