I have come across this problem generally and never really resolved it, but this morning I have a specific case.
My applications integrates with the Despatch Bay Shipping API: https://github.com/despatchbay/despatchbay-api-v15/wiki
I won't bother you with all the code, but this morning their endpoints are failing and I am getting an error on this line of my code, which requested shipping services from their API according to parcel data I send it:
Dim Services As dbShipping.ServiceType() = DespatchBay.Services.Get(Company, Box.ToParcel(ParcelValue:=ParcelTotal), DBRecipient)
Their code is also failing on their own website.
I have temporarily "overcome" this problem by wrapping the code that requires the Services
object above in a Try/Catch
but it takes quite a long time to actually fail.
So how do I, instead of writing:
Try
Dim Services As dbShipping.ServiceType() = DespatchBay.Services.Get(Company, Box.ToParcel(ParcelValue:=ParcelTotal), DBRecipient)
Catch
' Do stuff it broke
End Try
Write something like
Wait for Despatch Bay:
Dim Services As dbShipping.ServiceType() = DespatchBay.Services.Get(Company, Box.ToParcel(ParcelValue:=ParcelTotal), DBRecipient)
But if it takes too long
'Do stuff, it broke
End waiting for Despatch Bay
I only want to timeout the response from that API request, not my entire code block.
If it matters, I am looking for a .NetStandard
solution, not a Framework specific one.
I have found this Question similarly asked here:
Set timeout to an operation
My chosen solution is:
using System.Threading.Tasks;
Converted to VB and expecting my function return an object, my actual code is: