G
Guest
I have a program that using BeginInvoke on a Delegate to cause the Save to
execute on the background thread. When the call completes I have a callback
method which executes and updates the UI (after moving back to the main
thread). The problem I have is that if there was an error during the Save
method I want to update the UI differently, but I don't know how to tell from
my Callback method that an error occurred. Here is the basic snippets of
code
Class level delegate:
Private Delegate Function CreateDeductionRequestDelegate(ByVal amount As
Int32) As Int32
Here is my save routine. You will see that I have an error handler around
this method, which displays an error message to the user and logs the error.
Private Function CreateDeductionRequest(ByVal deductionRequestID As
Int32, ByVal deduction As OceanSpray.Trade.Deduction, ByVal filter As
OceanSpray.MasterData.FilterCollection, _
ByVal variableAmountToUse As Decimal, ByVal fixedAmountToUse
As Decimal, ByVal pooledFunds As Boolean) As Int32
Try
OceanSpray.Trade.DeductionRequest.CreateDeductionRequest(deductionRequestID,
deduction, filter, variableAmountToUse, fixedAmountToUse, pooledFunds)
Catch ex As Exception
LogError(ex)
End Try
End Function
In the Save button, you will see that my method I'm calling is call
CreateDeductionRequest and the name of the Callback method is SaveComplete.
I also have an AsyncState object which contains some information about what
I'm saving and is used in SaveComplete routine:
Dim CreateDeductionRequestRoutine As New
CreateDeductionRequestDelegate(AddressOf CreateDeductionRequest)
CreateDeductionRequestRoutine.BeginInvoke(100, _
AddressOf SaveComplete,
TradeDelegateAsyncState)
Finally I have the SaveComplete method which does some stuff to the UI, but
I need to know here if it failed in the Async method
Public Sub SaveComplete(ByVal ar As System.IAsyncResult) Implements
IMdiForm.SaveComplete
If Not Me.Status.InvokeRequired Then
Try
'Check if Save is from TradeFunction
OceanSpray.Common.Push()
If TypeOf ar.AsyncState Is
OceanSpray.Trade.TradeDelegateAsyncState Then
Dim TradeDelegateAsyncState As
OceanSpray.Trade.TradeDelegateAsyncState
'Do some stuff to update UI
Catch ex As Exception
LogError(ex)
Finally
OceanSpray.Common.Pop()
End Try
Else
Dim SaveRoutine As New AsyncCallback(AddressOf SaveComplete)
Me.BeginInvoke(SaveRoutine, New Object() {ar})
End If
End Sub
execute on the background thread. When the call completes I have a callback
method which executes and updates the UI (after moving back to the main
thread). The problem I have is that if there was an error during the Save
method I want to update the UI differently, but I don't know how to tell from
my Callback method that an error occurred. Here is the basic snippets of
code
Class level delegate:
Private Delegate Function CreateDeductionRequestDelegate(ByVal amount As
Int32) As Int32
Here is my save routine. You will see that I have an error handler around
this method, which displays an error message to the user and logs the error.
Private Function CreateDeductionRequest(ByVal deductionRequestID As
Int32, ByVal deduction As OceanSpray.Trade.Deduction, ByVal filter As
OceanSpray.MasterData.FilterCollection, _
ByVal variableAmountToUse As Decimal, ByVal fixedAmountToUse
As Decimal, ByVal pooledFunds As Boolean) As Int32
Try
OceanSpray.Trade.DeductionRequest.CreateDeductionRequest(deductionRequestID,
deduction, filter, variableAmountToUse, fixedAmountToUse, pooledFunds)
Catch ex As Exception
LogError(ex)
End Try
End Function
In the Save button, you will see that my method I'm calling is call
CreateDeductionRequest and the name of the Callback method is SaveComplete.
I also have an AsyncState object which contains some information about what
I'm saving and is used in SaveComplete routine:
Dim CreateDeductionRequestRoutine As New
CreateDeductionRequestDelegate(AddressOf CreateDeductionRequest)
CreateDeductionRequestRoutine.BeginInvoke(100, _
AddressOf SaveComplete,
TradeDelegateAsyncState)
Finally I have the SaveComplete method which does some stuff to the UI, but
I need to know here if it failed in the Async method
Public Sub SaveComplete(ByVal ar As System.IAsyncResult) Implements
IMdiForm.SaveComplete
If Not Me.Status.InvokeRequired Then
Try
'Check if Save is from TradeFunction
OceanSpray.Common.Push()
If TypeOf ar.AsyncState Is
OceanSpray.Trade.TradeDelegateAsyncState Then
Dim TradeDelegateAsyncState As
OceanSpray.Trade.TradeDelegateAsyncState
'Do some stuff to update UI
Catch ex As Exception
LogError(ex)
Finally
OceanSpray.Common.Pop()
End Try
Else
Dim SaveRoutine As New AsyncCallback(AddressOf SaveComplete)
Me.BeginInvoke(SaveRoutine, New Object() {ar})
End If
End Sub