Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Timer

Posted on 2006-06-17
4
Medium Priority
?
372 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 64

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

597 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