Async and Enterprise Services

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
 
S

S Gopikrishna

Hi,

After calling BeginInvoke use the following code to
execute any other functionality asynchronously

While Not ar.IsCompleted
'Logic for asynchronous operation
Wend

- Gopi
 
G

Guest

My problem is that the BeginInvoke does not seem to be
running async. It seems to take the amount of time I put
the thread to sleep (3 secs in this case).
 
T

Tian Min Huang

Hi,

Thanks for your post. Do you mean that BeginInvoke does not run
asynchronously for a component? I suggest you to sleep in the component
internal processing to check whether it is execute synchronously or it just
require some overhead for the execution.

I look forward to your result.

Have a nice day! :)

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

The sample that was attached in the original post
included a sleep and, yes, the amount of time that
BeginInvoke waits directly corresponds to the amount of
time the component sleeps. BeginInvoke, therefore, is
not running async. Why?
 
T

Tian Min Huang

Hi,

Thanks for your information. I am creating a sample project to check it on
my side, and will update you with my information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi,

I reproduced the problem on my side, and found that Serviced Components
does not support in process asynchronous calls. If you have an serviced
component in-proc and call its method asynchronously (through a BeginInvoke
on a delegate on the method), the BeginInvoke actually executes as a
complete call (because all custom proxies do not really have a way to
distinguish sync vs async calls).

In .NET Framwork 1.0, it throws exceptions any time we detect that an async
delegate invocation (Begin/End-Invoke) is happening on a Non-RemotingProxy.
While in .NET Framework 1.1, we now just wrap async behaviour around the
calls to BeginInvoke and EndInvoke in a non-RemotingProxy case instead of
throwing an exception.

In addition, I believe the following article is very helpful for
understanding Enterprise Services in .NET:
http://www.gotdotnet.com/team/xmlentsvcs/espaper.aspx

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top