VB.NET Threadsafe Events

Javin007
Javin007 used Ask the Experts™
on
I'm trying to get a threadsafe event to fire, but I can't seem to get it to work at all.  Here's the code I've tried using so far:
    Private Sub RaiseSafeEvent(ByVal sEvent As System.Delegate, ByVal EventArgArray As Object())
        If sEvent Is Nothing Then Exit Sub
        For Each delTemp As System.Delegate In sEvent.GetInvocationList
            Dim ISynchInvoke = CType(delTemp.Target, System.ComponentModel.ISynchronizeInvoke)
            If (Not ISynchInvoke Is Nothing) Then
                If (ISynchInvoke.InvokeRequired) Then ISynchInvoke.BeginInvoke(delTemp, EventArgArray)
            Else
                delTemp.DynamicInvoke(EventArgArray)
            End If
        Next
    End Sub

Open in new window


The problem is that when this fires, it seems to ALWAYS be as the thread is closing, which is utterly useless.  Here's where I call it in code:
        Debug.Print(Format(parTemp.ID, "00") & " = " & strTemp)
        RaiseSafeEvent(Lock_OnEvent, {parTemp.ID, strTemp, intTemp})

Open in new window


When the thread is created, the Debug.Print fires immediately, printing to the debug log.  But only after several seconds (when the thread has finished running) is it raising this event which is supposed to fire to let you know when the thread is STARTING.  

I may be going about this the complete wrong way, but this is driving me insane.  I'm about to just create a tight loop that monitors a variable and triggers an event whenever the variable changes... >:(

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The issue appears that it *may* not be strictly a thread issue.  Apparently at this point:

        RaiseEvent Lock_On(parTemp.ID, strTemp, intTemp)
        Debug.Print("Thread Is Starting")
        thrTemp.Start

The thread IS starting, but the event is NOT actually being raised in the parent object until AFTER the thread FINISHES running.  Suggestions?

Author

Commented:
Problem discovered to be code related.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial