?
Solved

Timer

Posted on 2006-06-17
4
Medium Priority
?
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 63

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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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