473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Raising an Event in a Delegate causes problems

I have an asynchronous call to a remote procedure that is invoked thru
BeginInvoke. The problem is that in the Delegate if I throw an event
before the CallBack function exits, the CallBack will get called multiple
times and error out with the following Error:

<EndInvoke can only be called once for each asynchronous operation.>

If I comment out the RaiseEvent, then the Callback exits with a single call
to EndDelegate.

Question: How can I throw an Event in the CallBack without causing a
problem
in the CallBack.

Many Thanks In Advance
'************************* Sample Code Snippet ****************************

Private Sub DataCallback(ByVal result As IAsyncResult)

Dim objResult As AsyncResult = CType(result, AsyncResult)
Dim objDelegate As FooDelegate = _
CType(objResult.AsyncDelegate, FooDelegate)

Dim bStatus As Boolean
Dim sFooData As String
bStatus = objDelegate.EndInvoke(sFooData, result)
m_checkList.Item(sCheckID).PCLStream = sPCLData
m_checkList.Item(sCheckID).Completed = True
m_checkList.Item(sCheckID).Errors = sErrorMsg
objDelegate = Nothing
RaiseEvent FooReceived(sFooData) '<<======= Causes DataCallBack
to be recalled 5 times
Debug.WriteLine("Result is <{0}>", bStatus.ToString)
End Sub
Nov 20 '05 #1
3 1107
Hi Jon,

can you post a complete working sample. I can not see any problem here.

Read this article for definition of sample :)

http://www.yoda.arachsys.com/csharp/complete.html

Sunny
In article <ui**************@TK2MSFTNGP11.phx.gbl>, jo********@mail.com
says...
I have an asynchronous call to a remote procedure that is invoked thru
BeginInvoke. The problem is that in the Delegate if I throw an event
before the CallBack function exits, the CallBack will get called multiple
times and error out with the following Error:

<EndInvoke can only be called once for each asynchronous operation.>

If I comment out the RaiseEvent, then the Callback exits with a single call
to EndDelegate.

Question: How can I throw an Event in the CallBack without causing a
problem
in the CallBack.

Many Thanks In Advance
'************************* Sample Code Snippet ****************************

Private Sub DataCallback(ByVal result As IAsyncResult)

Dim objResult As AsyncResult = CType(result, AsyncResult)
Dim objDelegate As FooDelegate = _
CType(objResult.AsyncDelegate, FooDelegate)

Dim bStatus As Boolean
Dim sFooData As String
bStatus = objDelegate.EndInvoke(sFooData, result)
m_checkList.Item(sCheckID).PCLStream = sPCLData
m_checkList.Item(sCheckID).Completed = True
m_checkList.Item(sCheckID).Errors = sErrorMsg
objDelegate = Nothing
RaiseEvent FooReceived(sFooData) '<<======= Causes DataCallBack
to be recalled 5 times
Debug.WriteLine("Result is <{0}>", bStatus.ToString)
End Sub

Nov 20 '05 #2

"Sunny" <su***@newsgroups.nospam> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
Hi Jon,

can you post a complete working sample. I can not see any problem here.
Sunny



Here is the complete code. The problem is in the CreateCheckPCLDelegate
function. When the event is thrown the callback
is reentered over and over again, up to about 4 times before the error
message appears.

'************************************************* **************************
******************************************

Class CheckPrintDelegate

Private Const CHECKPRINT_SERVER As String =
"http://localhost/PrintServices/CheckPrintServicesFBOD.rem"
Delegate Function CreateCheckPCLDelegate(ByRef CheckID As String, ByVal
XOffSet As Integer, ByVal YOffSet As Integer, ByRef PCLStream As Byte(),
ByRef ErrorMessage As String) As Boolean

Private m_xOffSet As Integer
Private m_yOffset As Integer
Private m_sRemoteURL As String
Private m_checkList As New CheckQueueList

Public Event CheckReceived(ByVal CheckID As String)
Private Sub New()

End Sub

Public Sub New(ByVal sRemoteURL As String)

Dim properties As New Hashtable
properties("name") = "HttpBinary"

m_sRemoteURL = sRemoteURL

Try
ChannelServices.RegisterChannel(New HttpChannel(properties, New
BinaryClientFormatterSinkProvider, Nothing))
' The last parameter above (Nothing) is the server sink provider
chain
' to obtain the default behavior (which includes SOAP and
' binary formatters on the server side).
Catch er As System.Runtime.Remoting.RemotingException
If er.Message.IndexOf("already registered") < 0 Then
Throw New ApplicationException(er.Message)
End If

Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

Public Property XOffset() As Integer
Get
Return (m_xOffSet)
End Get
Set(ByVal Value As Integer)
m_xOffSet = Value
End Set

End Property

Public Property YOffset() As Integer
Get
Return (m_yOffset)
End Get
Set(ByVal Value As Integer)
m_yOffset = Value
End Set

End Property

Public Sub ClearList()
m_checkList.Clear()
End Sub

Public ReadOnly Property CheckList() As CheckQueueList
Get
CheckList = m_checkList
End Get
End Property

Public Function GetDeclineLetterTemplate(ByRef sData As Byte(), ByRef
ErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean
Dim sStream As String

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus = objCheckPrintServices.GetDeclineLetterTemplate(sDa ta,
ErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetDeclineLetterTemplate():" &
ex.Message)
End Try

GetDeclineLetterTemplate = bStatus

End Function

Private Sub CreateCheckPCLCallback(ByVal result As IAsyncResult)

Dim objResult As AsyncResult = CType(result, AsyncResult)
Debug.WriteLine("AsyncResult = " & objResult.IsCompleted.ToString)
Dim objDelegate As CreateCheckPCLDelegate = _
CType(objResult.AsyncDelegate, CreateCheckPCLDelegate)

Dim bStatus As Boolean
Dim sPCLData As Byte()
Dim sCheckID As String
Dim sErrorMsg As String
bStatus = objDelegate.EndInvoke(sCheckID, sPCLData, sErrorMsg,
result)
m_checkList.Item(sCheckID).PCLStream = sPCLData
m_checkList.Item(sCheckID).Completed = True
m_checkList.Item(sCheckID).Errors = sErrorMsg
objDelegate = Nothing
RaiseEvent CheckReceived(sCheckID) '<<<========= Problem
is Here
Debug.WriteLine("Result is <{0}>", bStatus.ToString)
End Sub

Public Function GetMICRFont(ByRef sPCLStream As Byte()) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus =
objCheckPrintServices.CreateMICRFontPCLStream(sPCL Stream)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetMICRFont():" & ex.Message)
End Try

GetMICRFont = bStatus

End Function
Public Function GetTestPCLStream(ByVal CheckID As String, ByRef
sPCLStream As Byte(), ByRef sErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus =
objCheckPrintServices.CreateTestCheckPCLStream(Che ckID, m_xOffSet,
m_yOffset, sPCLStream, sErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetTestPCLStream():" &
ex.Message)
End Try

GetTestPCLStream = bStatus

End Function

Public Function GetPCLStream(ByVal CheckID As String, ByRef sPCLStream
As Byte(), ByRef sErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus = objCheckPrintServices.CreateCheckPCLStream(CheckID ,
m_xOffSet, m_yOffset, sPCLStream, sErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetPCLStream():" & ex.Message)
End Try

Return (bStatus)

End Function

Public Sub QueuePCLStream(ByVal CheckID As String)

Dim objCallback As New AsyncCallback(AddressOf
CreateCheckPCLCallback)
Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim result As IAsyncResult
Dim sPCLStream As Byte()
Dim sErrorMsg As String

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
Dim objDelegate As New CreateCheckPCLDelegate( _
AddressOf objCheckPrintServices.CreateTestCheckPCLStream)
'For Debuggin, Change from Test
m_checkList.Add(CheckID, Nothing, Nothing, False)
result = objDelegate.BeginInvoke(CheckID, m_xOffSet, m_yOffset,
sPCLStream, sErrorMsg, objCallback, _
Nothing)
'm_checkList.Add(CheckID, Nothing, Nothing, False)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("QueuePCLStream():" & ex.Message)
End Try

End Sub

Public Function GetQueuedCheckData(ByVal checkID As String) As
CheckQueueItem
'Note: This is a Volatile Get Function. It will return the
CheckQueueItem
' and then remove it from the list

GetQueuedCheckData = m_checkList.Item(checkID)
m_checkList.Remove(checkID)

End Function

End Class
Nov 20 '05 #3
Hi Jon,
as I'm c# guy, may I ask you to strip your code as much as possible. Leave
actually only the minimum number of methods and properties which reproduce
the problem.

Thanks
Sunny

Jon Turner wrote:

"Sunny" <su***@newsgroups.nospam> wrote in message
news:Oc**************@tk2msftngp13.phx.gbl...
Hi Jon,

can you post a complete working sample. I can not see any problem here.
Sunny


Here is the complete code. The problem is in the CreateCheckPCLDelegate
function. When the event is thrown the callback
is reentered over and over again, up to about 4 times before the error
message appears.

'************************************************* ************************** ******************************************

Class CheckPrintDelegate

Private Const CHECKPRINT_SERVER As String =
"http://localhost/PrintServices/CheckPrintServicesFBOD.rem"
Delegate Function CreateCheckPCLDelegate(ByRef CheckID As String,
ByVal
XOffSet As Integer, ByVal YOffSet As Integer, ByRef PCLStream As Byte(),
ByRef ErrorMessage As String) As Boolean

Private m_xOffSet As Integer
Private m_yOffset As Integer
Private m_sRemoteURL As String
Private m_checkList As New CheckQueueList

Public Event CheckReceived(ByVal CheckID As String)
Private Sub New()

End Sub

Public Sub New(ByVal sRemoteURL As String)

Dim properties As New Hashtable
properties("name") = "HttpBinary"

m_sRemoteURL = sRemoteURL

Try
ChannelServices.RegisterChannel(New HttpChannel(properties,
New
BinaryClientFormatterSinkProvider, Nothing))
' The last parameter above (Nothing) is the server sink
provider
chain
' to obtain the default behavior (which includes SOAP and
' binary formatters on the server side).
Catch er As System.Runtime.Remoting.RemotingException
If er.Message.IndexOf("already registered") < 0 Then
Throw New ApplicationException(er.Message)
End If

Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

Public Property XOffset() As Integer
Get
Return (m_xOffSet)
End Get
Set(ByVal Value As Integer)
m_xOffSet = Value
End Set

End Property

Public Property YOffset() As Integer
Get
Return (m_yOffset)
End Get
Set(ByVal Value As Integer)
m_yOffset = Value
End Set

End Property

Public Sub ClearList()
m_checkList.Clear()
End Sub

Public ReadOnly Property CheckList() As CheckQueueList
Get
CheckList = m_checkList
End Get
End Property

Public Function GetDeclineLetterTemplate(ByRef sData As Byte(), ByRef
ErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean
Dim sStream As String

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus =
objCheckPrintServices.GetDeclineLetterTemplate(sDa ta,
ErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetDeclineLetterTemplate():" &
ex.Message)
End Try

GetDeclineLetterTemplate = bStatus

End Function

Private Sub CreateCheckPCLCallback(ByVal result As IAsyncResult)

Dim objResult As AsyncResult = CType(result, AsyncResult)
Debug.WriteLine("AsyncResult = " & objResult.IsCompleted.ToString)
Dim objDelegate As CreateCheckPCLDelegate = _
CType(objResult.AsyncDelegate, CreateCheckPCLDelegate)

Dim bStatus As Boolean
Dim sPCLData As Byte()
Dim sCheckID As String
Dim sErrorMsg As String
bStatus = objDelegate.EndInvoke(sCheckID, sPCLData, sErrorMsg,
result)
m_checkList.Item(sCheckID).PCLStream = sPCLData
m_checkList.Item(sCheckID).Completed = True
m_checkList.Item(sCheckID).Errors = sErrorMsg
objDelegate = Nothing
RaiseEvent CheckReceived(sCheckID) '<<<=========
Problem
is Here
Debug.WriteLine("Result is <{0}>", bStatus.ToString)
End Sub

Public Function GetMICRFont(ByRef sPCLStream As Byte()) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus =
objCheckPrintServices.CreateMICRFontPCLStream(sPCL Stream)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetMICRFont():" & ex.Message)
End Try

GetMICRFont = bStatus

End Function
Public Function GetTestPCLStream(ByVal CheckID As String, ByRef
sPCLStream As Byte(), ByRef sErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus =
objCheckPrintServices.CreateTestCheckPCLStream(Che ckID, m_xOffSet,
m_yOffset, sPCLStream, sErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetTestPCLStream():" &
ex.Message)
End Try

GetTestPCLStream = bStatus

End Function

Public Function GetPCLStream(ByVal CheckID As String, ByRef sPCLStream
As Byte(), ByRef sErrorMessage As String) As Boolean

Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim bStatus As Boolean

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
bStatus = objCheckPrintServices.CreateCheckPCLStream(CheckID ,
m_xOffSet, m_yOffset, sPCLStream, sErrorMessage)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("GetPCLStream():" & ex.Message)
End Try

Return (bStatus)

End Function

Public Sub QueuePCLStream(ByVal CheckID As String)

Dim objCallback As New AsyncCallback(AddressOf
CreateCheckPCLCallback)
Dim objCheckPrintServices As LTSWebChecks.CheckPrintServicesFBOD
Dim result As IAsyncResult
Dim sPCLStream As Byte()
Dim sErrorMsg As String

Try
objCheckPrintServices =
CType(Activator.GetObject(GetType(LTSWebChecks.Che ckPrintServicesFBOD),
m_sRemoteURL), LTSWebChecks.CheckPrintServicesFBOD)
Dim objDelegate As New CreateCheckPCLDelegate( _
AddressOf objCheckPrintServices.CreateTestCheckPCLStream)
'For Debuggin, Change from Test
m_checkList.Add(CheckID, Nothing, Nothing, False)
result = objDelegate.BeginInvoke(CheckID, m_xOffSet,
m_yOffset,
sPCLStream, sErrorMsg, objCallback, _
Nothing)
'm_checkList.Add(CheckID, Nothing, Nothing, False)
Catch ex As Exception
Debug.Write(ex.Message)
Throw New ApplicationException("QueuePCLStream():" &
ex.Message)
End Try

End Sub

Public Function GetQueuedCheckData(ByVal checkID As String) As
CheckQueueItem
'Note: This is a Volatile Get Function. It will return the
CheckQueueItem
' and then remove it from the list

GetQueuedCheckData = m_checkList.Item(checkID)
m_checkList.Remove(checkID)

End Function

End Class


Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Santhoshi | last post by:
I have an event declared in class1 Now i want to raise the event in the my class2 or anywhere other than class1 Is it possible? If so how Thank you
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
2
by: Catherine Jones | last post by:
Dear All Please help me in the following issue: how to raise an event from an unmanaged class to a managed class. Both classes are in C++ and there should not be any static methods involved in...
3
by: Jon Turner | last post by:
I have an asynchronous call to a remote procedure that is invoked thru BeginInvoke. The problem is that in the Delegate if I throw an event before the CallBack function exits, the CallBack will...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Tina | last post by:
I am trying to raise a custom event in a user control passing custom data arguments. I have done this many times in VB but this is my first time in C#. The code below passes the proper...
5
by: kmcmanus | last post by:
I have just started to write a few business classes that are largely made up of properties (getters and setters). For each setter I want to fire a changed event - each event will have a unique...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.