G
Guest
I'm currently trying to solve a problem that has evaded me for months now. I
am attempting to raise an event from a custom class that can be handled by
the main UI.
Here's an example of my class:
Imports System.Threading
Public Class ASyncTestObject
Private Delegate Sub ThreadEndDelegate(ByRef e As
ASyncTestObject.ASyncTestObjectEventArgs)
Private D As New ThreadEndDelegate(AddressOf OnThreadEnd)
Public Event ThreadEnd(ByVal sender As Object, ByVal e As
ASyncTestObjectEventArgs)
Private _W As Thread
Private _e As ASyncTestObjectEventArgs
Public Class ASyncTestObjectEventArgs
Inherits EventArgs
End Class
Private Delegate Sub Callback(ByVal e As IAsyncResult)
Protected Sub OnThreadEnd(ByRef e As ASyncTestObjectEventArgs)
Trace.WriteLine("Raising event from " & Thread.CurrentThread.Name)
_e = e
RaiseEvent ThreadEnd(Me, _e)
End Sub
Private Sub Work()
Trace.WriteLine("New ASyncObject working from " &
Thread.CurrentThread.Name)
_W.Sleep(5000)
D(New ASyncTestObjectEventArgs)
End Sub
Public Sub New()
Trace.WriteLine("New ASyncObject created by " &
Thread.CurrentThread.Name)
_W = New Thread(AddressOf Work)
_W.Name = "Worker Thread"
_e = New ASyncTestObjectEventArgs
End Sub
Public Sub StartWork()
Trace.WriteLine("New ASyncObject started on " &
Thread.CurrentThread.Name)
_W.Start()
End Sub
End Class
However, as I follow my code, the Trace.WriteLine method identifies that the
object is being called from the Worker Thread. As I handle the event in a
console application, the event is being handled on the Worker Thread instead
of the Main Thread.
Here's the example of my console application test:
Imports ASyncObject
Module Module1
Private WithEvents A As ASyncTestObject
Sub Main()
System.Threading.Thread.CurrentThread.Name = "Main Thread"
A = New ASyncTestObject
Console.WriteLine("Starting worker process...")
A.StartWork()
Console.WriteLine("Worker process working...")
Console.ReadLine()
End Sub
Private Sub A_Done(ByVal sender As Object, ByVal e As
ASyncTestObject.ASyncTestObjectEventArgs) Handles A.ThreadEnd
Trace.WriteLine("New ASyncObject.ThreadDone handled by " &
System.Threading.Thread.CurrentThread.Name)
Console.WriteLine("Worker process completed.")
End Sub
End Module
Any insights or resources would be welcome as I have struck out trying to
find resources on my own.
Thanks!
am attempting to raise an event from a custom class that can be handled by
the main UI.
Here's an example of my class:
Imports System.Threading
Public Class ASyncTestObject
Private Delegate Sub ThreadEndDelegate(ByRef e As
ASyncTestObject.ASyncTestObjectEventArgs)
Private D As New ThreadEndDelegate(AddressOf OnThreadEnd)
Public Event ThreadEnd(ByVal sender As Object, ByVal e As
ASyncTestObjectEventArgs)
Private _W As Thread
Private _e As ASyncTestObjectEventArgs
Public Class ASyncTestObjectEventArgs
Inherits EventArgs
End Class
Private Delegate Sub Callback(ByVal e As IAsyncResult)
Protected Sub OnThreadEnd(ByRef e As ASyncTestObjectEventArgs)
Trace.WriteLine("Raising event from " & Thread.CurrentThread.Name)
_e = e
RaiseEvent ThreadEnd(Me, _e)
End Sub
Private Sub Work()
Trace.WriteLine("New ASyncObject working from " &
Thread.CurrentThread.Name)
_W.Sleep(5000)
D(New ASyncTestObjectEventArgs)
End Sub
Public Sub New()
Trace.WriteLine("New ASyncObject created by " &
Thread.CurrentThread.Name)
_W = New Thread(AddressOf Work)
_W.Name = "Worker Thread"
_e = New ASyncTestObjectEventArgs
End Sub
Public Sub StartWork()
Trace.WriteLine("New ASyncObject started on " &
Thread.CurrentThread.Name)
_W.Start()
End Sub
End Class
However, as I follow my code, the Trace.WriteLine method identifies that the
object is being called from the Worker Thread. As I handle the event in a
console application, the event is being handled on the Worker Thread instead
of the Main Thread.
Here's the example of my console application test:
Imports ASyncObject
Module Module1
Private WithEvents A As ASyncTestObject
Sub Main()
System.Threading.Thread.CurrentThread.Name = "Main Thread"
A = New ASyncTestObject
Console.WriteLine("Starting worker process...")
A.StartWork()
Console.WriteLine("Worker process working...")
Console.ReadLine()
End Sub
Private Sub A_Done(ByVal sender As Object, ByVal e As
ASyncTestObject.ASyncTestObjectEventArgs) Handles A.ThreadEnd
Trace.WriteLine("New ASyncObject.ThreadDone handled by " &
System.Threading.Thread.CurrentThread.Name)
Console.WriteLine("Worker process completed.")
End Sub
End Module
Any insights or resources would be welcome as I have struck out trying to
find resources on my own.
Thanks!