Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading and Timers 1

Status
Not open for further replies.

bjd4jc

Programmer
Nov 8, 2001
1,627
US
I have an application written to monitor some direcotories for XML files and create a fax from them.

I decided to create threading in this application because there are multiple directories and lots of files in each directory.

I have a FaxTemplate that stores the info on which directory to process. Inside of this class I have a timer (because they want it to pause for a while between directory sweeps). I have a class which processes these directories. It has a ProcessDirectory method.

Here is the code in the FaxTemplate class:

Code:
    Private RunningThread As Thread

    Private Sub mTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles mTimer.Tick
        mTimer.Stop()

        RaiseEvent Processing(Me)
        Console.WriteLine("Timer_Tick: " & Me.TemplateName)
        mDirProcessor = New DirProcessor(Me)
        RunningThread = New Thread(AddressOf mDirProcessor.ProcessDirectory)
        RunningThread.Name = Me.TemplateName
        RunningThread.Start()
    End Sub

    Public Sub StartRead()
        mTimer = New System.windows.Forms.Timer
        mTimer.Interval = 10000 '300000 '5 minutes
        mTimer.Start()
    End Sub

    Private Sub DirectoryProcessComplete() Handles mDirProcessor.ProcessCompleted
        Console.WriteLine("ProcessComplete: " & Me.TemplateName)
        RunningThread = Nothing
        StartRead()
        RaiseEvent ProcessComplete(Me)
    End Sub

Here is the code in the ProcessDirectory class:

Code:
    Public Sub ProcessDirectory()
        Dim File As String

        If Not Directory.Exists(mDirectoryName) Then
            RaiseEvent ProcessError("Directory " & mDirectoryName & " does not exists.")
            Exit Sub
        End If

        Try
            For Each File In Directory.GetFiles(mDirectoryName)
                If Right(File, 4).ToUpper = ".XML" Then
                    ProcessXMLDocument(File)
                    FileSystem.Kill(File)
                End If
            Next
        Catch ex As Exception
            RaiseEvent ProcessError(ex.Message)
        End Try

        RaiseEvent ProcessCompleted()
    End Sub

Everything runs fine the first time through but it seems that my timer is not getting reset and it is not processing the direcotry more than once. If I take out the threading piece, it will keep processing the directories time after time.

Anyone know why this is happening?

Thanks
 
Check out the FileSystemWatcher class. It may help you out!

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
OK. I think I can make that work. Have a star.

I will conver to the FileSystemWatcher class, but do you have any idea what I was doing wrong in the code above?

Thanks a lot!

Brian
 
I think in the StartRead method, when you set mTimer = to a new instance you lose the event handler that is defined for mTimer.tick

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I got it all to work great with the FileSystemWatcher class!! Thanks a lot for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top