D
Dave
I'm using the BeginInvoke method of a delegate to invoke
a thread asynchronously and then use the EndInvoke to
retrieve the value. This works wonderfully until a
Serviced Component is added to the mix. When the
Serviced Component is invoked with BeginInvoke, the code
hangs for the duration of the call rather than running
async. Any ideas? Sample code below:
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
'Purpose: Submit 2 components that share the same
' module-level variables. Attempt to
show
' that COM+ can solve synchronization
problems.
Dim x1 As New myComClass
Dim x2 As New myComClass
'Prepare the first bad boy.
Dim del As TedsHandler
del = AddressOf x1.StartCounting
Dim ar As IAsyncResult
'Prepare the second bad boy.
Dim del2 As TedsHandler
del2 = AddressOf x2.StartCounting
Dim ar2 As IAsyncResult
'Let em run. Each BeginInvoke takes
'3 seconds (due to the lag in the component).
'I expected these to run async. Why didn't they?
ar = del.BeginInvoke(Nothing, Nothing)
ar2 = del2.BeginInvoke(Nothing, Nothing)
'Any code here will also run async.
'Wait for them both to return.
Dim l1 As Long = del.EndInvoke(ar)
Label1.Text = l1.ToString
Dim l2 As Long = del2.EndInvoke(ar2)
Label2.Text = l2.ToString
x1.Dispose()
x1 = Nothing
x2.Dispose()
x2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
'Purpose: Submit 2 components that share the same
' module-level variables. Attempt to
show
' syncronization problems.
Dim x1 As New myBadClass
Dim x2 As New myBadClass
'Prepare the first bad boy.
Dim del As TedsHandler
del = AddressOf x1.StartCounting
Dim ar As IAsyncResult
'Prepare the second bad boy.
Dim del2 As TedsHandler
del2 = AddressOf x2.StartCounting
Dim ar2 As IAsyncResult
'Let em run. Async works and illustrates
ar = del.BeginInvoke(Nothing, Nothing)
ar2 = del2.BeginInvoke(Nothing, Nothing)
'Any code here will also run async.
'Wait for them both to return.
Dim l1 As Long = del.EndInvoke(ar)
Label1.Text = l1.ToString
Dim l2 As Long = del2.EndInvoke(ar2)
Label2.Text = l2.ToString
x1 = Nothing
x2 = Nothing
End Sub
End Class
Public Class myBadClass
'Without EnterpriseServices - Works grate but has
'the sync problem with the Shared member.
Private Shared mlModuleLevelCounter As Long = 0
Public Function StartCounting() As Long
Dim l As Long = mlModuleLevelCounter + 1
System.Threading.Thread.CurrentThread.Sleep(3000)
mlModuleLevelCounter = l
Return l
End Function
End Class
<Synchronization(SynchronizationOption.Required),
EventTrackingEnabled(True)> _
Public Class myComClass
Inherits ServicedComponent
Private Shared mlModuleLevelCounter As Long = 0
Public Function StartCounting() As Long
Dim l As Long = mlModuleLevelCounter + 1
System.Threading.Thread.CurrentThread.Sleep(3000)
mlModuleLevelCounter = l
Return l
End Function
End Class
Public Module myModule
Public Delegate Function TedsHandler() As Long
End Module
a thread asynchronously and then use the EndInvoke to
retrieve the value. This works wonderfully until a
Serviced Component is added to the mix. When the
Serviced Component is invoked with BeginInvoke, the code
hangs for the duration of the call rather than running
async. Any ideas? Sample code below:
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
'Purpose: Submit 2 components that share the same
' module-level variables. Attempt to
show
' that COM+ can solve synchronization
problems.
Dim x1 As New myComClass
Dim x2 As New myComClass
'Prepare the first bad boy.
Dim del As TedsHandler
del = AddressOf x1.StartCounting
Dim ar As IAsyncResult
'Prepare the second bad boy.
Dim del2 As TedsHandler
del2 = AddressOf x2.StartCounting
Dim ar2 As IAsyncResult
'Let em run. Each BeginInvoke takes
'3 seconds (due to the lag in the component).
'I expected these to run async. Why didn't they?
ar = del.BeginInvoke(Nothing, Nothing)
ar2 = del2.BeginInvoke(Nothing, Nothing)
'Any code here will also run async.
'Wait for them both to return.
Dim l1 As Long = del.EndInvoke(ar)
Label1.Text = l1.ToString
Dim l2 As Long = del2.EndInvoke(ar2)
Label2.Text = l2.ToString
x1.Dispose()
x1 = Nothing
x2.Dispose()
x2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
'Purpose: Submit 2 components that share the same
' module-level variables. Attempt to
show
' syncronization problems.
Dim x1 As New myBadClass
Dim x2 As New myBadClass
'Prepare the first bad boy.
Dim del As TedsHandler
del = AddressOf x1.StartCounting
Dim ar As IAsyncResult
'Prepare the second bad boy.
Dim del2 As TedsHandler
del2 = AddressOf x2.StartCounting
Dim ar2 As IAsyncResult
'Let em run. Async works and illustrates
ar = del.BeginInvoke(Nothing, Nothing)
ar2 = del2.BeginInvoke(Nothing, Nothing)
'Any code here will also run async.
'Wait for them both to return.
Dim l1 As Long = del.EndInvoke(ar)
Label1.Text = l1.ToString
Dim l2 As Long = del2.EndInvoke(ar2)
Label2.Text = l2.ToString
x1 = Nothing
x2 = Nothing
End Sub
End Class
Public Class myBadClass
'Without EnterpriseServices - Works grate but has
'the sync problem with the Shared member.
Private Shared mlModuleLevelCounter As Long = 0
Public Function StartCounting() As Long
Dim l As Long = mlModuleLevelCounter + 1
System.Threading.Thread.CurrentThread.Sleep(3000)
mlModuleLevelCounter = l
Return l
End Function
End Class
<Synchronization(SynchronizationOption.Required),
EventTrackingEnabled(True)> _
Public Class myComClass
Inherits ServicedComponent
Private Shared mlModuleLevelCounter As Long = 0
Public Function StartCounting() As Long
Dim l As Long = mlModuleLevelCounter + 1
System.Threading.Thread.CurrentThread.Sleep(3000)
mlModuleLevelCounter = l
Return l
End Function
End Class
Public Module myModule
Public Delegate Function TedsHandler() As Long
End Module