We help IT Professionals succeed at work.
Get Started

VB.NET Threadsafe Events

Javin007
Javin007 asked
on
486 Views
Last Modified: 2012-05-10
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
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE