Is RaiseEvent Synchronous or Asynchronous in VB .Net ??

G

Guest

Hi,

I believe that when an event is Raised is C#, it is performed synchronously, as a value is returned by the event handler, even if that value is NULL. However, as VB does not have the ability to receive a return value, I was wondering is the call was performed asynchronously, rather than blocking on a call for which it would not receive a response.

I was specifically thinking of the case of having a cient subscribing to the event in a server, and the server being present in a remote machine.

Al
 
M

Mattias Sjögren

Raising an event calls the handlers synchronously, regardless of
language.



Mattias
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?QWw=?= said:
I believe that when an event is Raised is C#, it is performed
synchronously, as a value is returned by the event handler, even if that
value is NULL. However, as VB does not have the ability to receive a
return value, I was wondering is the call was performed asynchronously,
rather than blocking on a call for which it would not receive a
response.

The 'RaiseEvent' statement is processed as a call to the 'Invoke' method
of the event's delegate. This is a synchronous operation.
 
J

Jay B. Harlow [MVP - Outlook]

Al,
In addition to the

Events in .NET any language are processed synchronously.

I believe because to be CLS compliant events do not return values, thus VB
does not allow a return value for an event. Also events by definition are
multicast, if the event returned a value how would each handler return its
value where there are more then one handler??

What I would do is follow the Event Pattern & define my event as a Sub with
Object & EventArgs parameters and make a custom EventArgs that has the
"return value" in it. (Similar to the
System.ComponentModel.CancelEventHandler)

Something like:

Public Class GetDataEventArgs
Inherits EventArgs

Private m_data As Integer

Public Property Data() As Integer
Get
Return m_data
End Get
Set(ByVal value As Integer)
m_data = value
End Set
End Property

End Class

Public Delegate Sub GetDataHandler(ByVal sender As Object, ByVal e As
GetDataEventArgs)

Public Class MyProcess

Public Event GetData As GetDataHandler

Protected Overridable Sub OnGetData(ByVal e As GetDataEventArgs)
RaiseEvent GetData(Me, e)
End Sub

Public Sub Process()
Dim e As New GetDataEventArgs
OnGetData(e)
If e.Data = 100 Then
' do something interesting
End If
End Sub

End Class



Hope this helps
Jay

Al said:
Hi,

I believe that when an event is Raised is C#, it is performed
synchronously, as a value is returned by the event handler, even if that
value is NULL. However, as VB does not have the ability to receive a return
value, I was wondering is the call was performed asynchronously, rather than
blocking on a call for which it would not receive a response.
I was specifically thinking of the case of having a cient subscribing to
the event in a server, and the server being present in a remote machine.
 

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