471,311 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

RaiseEvent not working

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)
With Me
.lblCount.Text = ""
.lblMsg.Text = ""
.lblTitle.Text = ""
.pb.Value = 0
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)
End Sub
Private Sub SynchronizeDirs(ByVal State As Object)
'Perform Long Running Process..
RaiseEvent Done(Proc.SynchronizeDirs)
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
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
SetTitle("Adding new files to list...")
For Each fmdir As String In TargetList
Dim di As New DirectoryInfo(fmdir)
Dim dif() As FileInfo = di.GetFiles("*.zip",
'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 & "')"
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
For Each r As DataRow In dt.Rows
SetMsg(r.Item("Directory") & "\" & r.Item("Filename").ToString)
If File.Exists(r.Item("Directory").ToString & "\" &
r.Item("Filename").ToString) = False Then
End If
RaiseEvent Done(Proc.SynchronizeDirs)
End Sub
May 20 '07 #1
1 1943
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".
May 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Nicolas | last post: by
2 posts views Thread by Carl tam | last post: by
2 posts views Thread by Lim | last post: by
3 posts views Thread by Adam | last post: by
2 posts views Thread by dmoonme | last post: by
7 posts views Thread by Onokiyo | last post: by
2 posts views Thread by ffa | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.