By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,650 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,650 IT Pros & Developers. It's quick & easy.

Async and Enterprise Services

P: n/a
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



Jul 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

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

While Not ar.IsCompleted
'Logic for asynchronous operation
Wend

- Gopi
-----Original Message-----
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



.

Jul 21 '05 #2

P: n/a
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).
-----Original Message-----
Hi,

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

While Not ar.IsCompleted
'Logic for asynchronous operation
Wend

- Gopi
-----Original Message-----
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



.

.

Jul 21 '05 #3

P: n/a
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.

Jul 21 '05 #4

P: n/a
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?
-----Original Message-----
Hi,

Thanks for your post. Do you mean that BeginInvoke does not runasynchronously for a component? I suggest you to sleep in the componentinternal processing to check whether it is execute synchronously or it justrequire 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.
.

Jul 21 '05 #5

P: n/a
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.

Jul 21 '05 #6

P: n/a
Thanks! Any help is greatly appreciated!
-----Original Message-----
Hi,

Thanks for your information. I am creating a sample project to check it onmy 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.
.

Jul 21 '05 #7

P: n/a
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.

Jul 21 '05 #8

P: n/a
Thanks for the info!
-----Original Message-----
Hi,

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

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

In addition, I believe the following article is very helpful forunderstanding 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.
.

Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.