Link to home
Start Free TrialLog in
Avatar of chlade
chlade

asked on

Stopping a thread that contains a timer

I have a windows form.  On this form is a button.  When the user clicks the button, it should open a form (call it Form2).  This form starts a thread that will monitor a folder.  I created a timer to wake up every 5 seconds to check the folder for new files.  When Form2 is closed, the thread should stop.  However, it does not seem to stop.  The Elapsed event keeps on firing.

I created a small test app that simulates this.  How can I make this work as I intend?

You can paste the code below into a new WinForms app and see how it currently works.

Thanks,
Chris

Imports System.Threading
 
Public Class frmMapper
 
    Friend WithEvents Button1 As System.Windows.Forms.Button
 
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Button1 = New System.Windows.Forms.Button
        Me.Controls.Add(Me.Button1)
         
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim frm As New frmMonitorTester2
        frm.Show()
 
    End Sub
 
    Public Class frmMonitorTester2
        Inherits System.Windows.Forms.Form
 
        Private WithEvents oWorkerThread As WorkerThread
        Private oThread As Thread
 
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
            oWorkerThread = New WorkerThread()
            oThread = New Thread(New ThreadStart(AddressOf oWorkerThread.ProcessFiles))
            oThread.Start()
 
        End Sub
 
        Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
 
            oThread.Abort()
            oThread.Join()
            oThread = Nothing
            oWorkerThread = Nothing
 
        End Sub
 
    End Class
 
    Public Class WorkerThread
 
        Private WithEvents Timer1 As New System.Timers.Timer
 
        Public Sub ProcessFiles()
            Timer1.Interval = 5000
            Timer1.Enabled = True
        End Sub
 
        Protected Sub Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
            Console.WriteLine("doing something ... " & Now.ToString("HH:mm:ss"))
        End Sub
 
    End Class
 
End Class

Open in new window

Avatar of ZachSmith
ZachSmith
Flag of United States of America image

A better approach to monitoring files is to use the FileSystemMonitor class.
Also, is there a reason why you want the Timer in it's own thread? The Timer object creates a thread internally, so I'm not clear on why you are creating a thread for it. If you just used the Timer you could simply call Timer.Enabled = false to stop it from checking the directory.
Avatar of chlade
chlade

ASKER

Well, I simplified the example here.  I am monitoring a folder, a SQL table, and a few other things.  My question remains the same.  How can I start a thread that will run indefinitely with a timer firing at specified intervals and be able to be stopped on demand.
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chlade

ASKER

While I'm still a little curious whether I would be able to do what I was trying, I was able to skip using a thread in this situation and I do like that solution better.