Solved

Timer

Posted on 2006-06-17
4
369 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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