RaiseEvent not working

T

Terry Olsen

I have a program with a couple of long running processes that i'm calling on
a separate thread.

When the process is completed, I want to raise an event to tell the main
thread that it's done. I set it up this way...

Public Class frmLongRunningProcess

Public Enum Proc
ParseZipFiles = 0
TestZipFiles = 1
GetWebLinks = 2
SynchronizeDirs = 3
End Enum

Public Event Done(ByVal ProcDone As Proc)

Public Sub New(ByVal PerformProc As Proc, ByVal TargetList As ArrayList)
InitializeComponent()
With Me
.lblCount.Text = ""
.lblMsg.Text = ""
.lblTitle.Text = ""
.pb.Value = 0
.Show()
End With

Select Case PerformProc
Case Proc.TestZipFiles
ThreadPool.QueueUserWorkItem(AddressOf TestZipFiles, TargetList)
Case Proc.SynchronizeDirs
ThreadPool.QueueUserWorkItem(AddressOf SynchronizeDirs, TargetList)
End Select
End Sub

Private Sub TestZipFiles(ByVal state As Object)
'Perform Long Running Process..
RaiseEvent Done(Proc.TestZipFiles)
CloseForm()
End Sub


Private Sub SynchronizeDirs(ByVal State As Object)
'Perform Long Running Process..
RaiseEvent Done(Proc.SynchronizeDirs)
CloseForm()
End Sub

End Class

Then, in my main program, I have the following code:

Dim WithEvents LongRunProc As frmLongRunningProcess
Private Sub LongRunProc_Done(ByVal e As frmLongRunningProcess.Proc) Handles
LongRunProc.Done
MsgBox(e.ToString)
End Sub

When I run the "TestZipFiles" process, the event gets raised and I see the
expected MessageBox. But When I run the "SynchronizeDirs" process, the event
doesn't get raised. If I change the "RaiseEvent" line in the "TestZipFiles"
routine to "Proc.SynchronizeDirs", the event gets raised as expected. It is
only the "SynchronizeDirs" routine that does not raise the event. Both
routines close the form as expected. Since it might help to see all the code
in that routine, i'll post it below:

Private Sub SynchronizeDirs(ByVal State As Object)
Dim TargetList As ArrayList = State
UpdateProgressBar(0)
SetTitle("Adding new files to list...")
For Each fmdir As String In TargetList
UpdateProgressBar(0)
Dim di As New DirectoryInfo(fmdir)
SetMsg(fmdir)
Dim dif() As FileInfo = di.GetFiles("*.zip",
SearchOption.TopDirectoryOnly)
SetProgressBarMax(dif.Length)
'Make sure each file in the directory is in the database.
For i As Integer = 0 To dif.Length - 1
Dim fi As FileInfo = dif(i)
SetMsg(fmdir & "\" & fi.Name)
UpdateProgressBar(i + 1)
Dim cmd As String = "SELECT COUNT(*) FROM FMFiles WHERE
[Filename] = '" & fi.Name & "' AND [Directory] = '" & fmdir & "'"
If MDBScalar(cmd) > 0 Then Continue For
cmd = "INSERT INTO FMFiles (Filename,[FileSize (MB)],Directory)
VALUES (" & _
"'" & fi.Name & "','" &
Double.Parse(System.Math.Round((fi.Length / 1024) / 1000, 2),
CI.NumberFormat) & "'," & _
"'" & fmdir & "')"
MDBNonquery(cmd)
Next
Next
UpdateProgressBar(0)
SetTitle("Removing deleted files from list...")
'Kill any database entries that aren't in a directory
Dim dt As DataTable = MDBQuery("SELECT ID,Filename,Directory FROM
FMFiles")
SetProgressBarMax(dt.Rows.Count)
UpdateProgressBar(0)
For Each r As DataRow In dt.Rows
IncrementProgressBar()
SetMsg(r.Item("Directory") & "\" & r.Item("Filename").ToString)
If File.Exists(r.Item("Directory").ToString & "\" &
r.Item("Filename").ToString) = False Then
MDBNonquery("DELETE FROM FMFiles WHERE ID=" &
r.Item("ID").ToString)
End If
Next
RaiseEvent Done(Proc.SynchronizeDirs)
CloseForm()
End Sub
 
T

Terry Olsen

Nevermind. Figured it out.

It would help if I used the actual variable I setup using WithEvents instead
of instinctively doing a "dim x as new LongProc".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top