Threading Question

  • Thread starter Thread starter Jason MacKenzie
  • Start date Start date
J

Jason MacKenzie

I'm attempting to write data to our SAP system in VB.Net. The problem is
that in certain situations, my app will hang until SAP has resources
available. This causes huge problems for us the issue isn't noticed until
our inventory people call in a panic.

Is there a way I can use threading to throw an exception if its been hung on
that line for over a minute?

If this is not applicable I'm open to any suggestions.

Thanks,

Jason
 
Jason MacKenzie said:
I'm attempting to write data to our SAP system in VB.Net. The problem is
that in certain situations, my app will hang until SAP has resources
available. This causes huge problems for us the issue isn't noticed until
our inventory people call in a panic.

Is there a way I can use threading to throw an exception if its been hung on
that line for over a minute?

You can turn any synchronous function call into an asynchronous function
simply by wrapping it in a delegate.

Then instead of invoking the function directly use the delegate's
BeginInvoke, do whatever you want while you wait, and when you finally need
to wait until the function ends run EndInvoke.


David


Like this:




Module Module1
Function MyFunction(ByVal input1 As String, ByVal input2 As Integer) As
Integer
System.Threading.Thread.Sleep(1000 * 30)
End Function
Private Delegate Function MyFunctionDelegateType(ByVal input1 As String,
ByVal input2 As Integer) As Integer
Dim MyFunctionDelegate As MyFunctionDelegateType = AddressOf MyFunction

Sub main()
Dim asyncHandle As System.IAsyncResult =
MyFunctionDelegate.BeginInvoke("abc", 123, Nothing, Nothing)

'wait for 15 seconds and show a message if the function hasn't completed
If Not asyncHandle.AsyncWaitHandle.WaitOne(1000 * 15, False) Then
Console.WriteLine("long running method has been running for 15
seconds")
End If

'wait until the function ends
Dim result As Integer = MyFunctionDelegate.EndInvoke(asyncHandle)
End Sub



End Module
 
Thank you very much


David Browne said:
hung

You can turn any synchronous function call into an asynchronous function
simply by wrapping it in a delegate.

Then instead of invoking the function directly use the delegate's
BeginInvoke, do whatever you want while you wait, and when you finally need
to wait until the function ends run EndInvoke.


David


Like this:




Module Module1
Function MyFunction(ByVal input1 As String, ByVal input2 As Integer) As
Integer
System.Threading.Thread.Sleep(1000 * 30)
End Function
Private Delegate Function MyFunctionDelegateType(ByVal input1 As String,
ByVal input2 As Integer) As Integer
Dim MyFunctionDelegate As MyFunctionDelegateType = AddressOf MyFunction

Sub main()
Dim asyncHandle As System.IAsyncResult =
MyFunctionDelegate.BeginInvoke("abc", 123, Nothing, Nothing)

'wait for 15 seconds and show a message if the function hasn't completed
If Not asyncHandle.AsyncWaitHandle.WaitOne(1000 * 15, False) Then
Console.WriteLine("long running method has been running for 15
seconds")
End If

'wait until the function ends
Dim result As Integer = MyFunctionDelegate.EndInvoke(asyncHandle)
End Sub



End Module
 
Back
Top