This method allows you to call any .NET async
method - any method that returns a Task
object basically - asynchronously by passing in a callback object that is called back when the async method completes or fails.
This method provides true, out of band async processing using callbacks handlers rather than linear code, but it allows you to continue running code in FoxPro until the async code completes and notifies you in the background.
Requires a Callback Handler Object
Unlike linear code this method requires a callback method to be notified when the async call succeeds or fails. It this via an AsyncCallbackEvents
class you can implement that handles OnCompleted()
and OnError()
methods that notify of success or failure in the background as the operation completes.
Keep Callback Handlers simple
It's important that the callback handler methods should not be long running as they block in .NET, and - if many are running simultaneously calling into FoxPro STA objects - you can get into potential deadlock states. This feature is pushing FoxPro's threading logic to its limit and officially this type of callback scenario is not recommended. However, as long as you keep the callback methods fast and as non-blocking as possible this scheme works reliably.
It's recommended that if you do any significant code you schedule that code onto the main FoxPro thread. So instead of doing complex processing in the callback handler, pick up the result value and store it in an accessible property or variable, and then trigger processing from your main FoxPro thread. This minimizes the code that runs off a non-FoxPro .NET thread and prevents blocking on the .NET end.
o.wwDotNetBridge.InvokeTaskMethodAsync(loCallback, loInstance, loMethod)
Parameters
loCallback
A callback object that inherits from AsyncCallbackEvents
and implements
Example
* This sample demonstrates running 2 HTTP requests simultaneously
CLEAR
do wwDotNetBridge
LOCAL loBridge as wwDotNetBridge
loBridge = CreateObject("wwDotNetBridge","V4")
loClient = loBridge.CreateInstance("System.Net.WebClient")
* Optional - go through Fiddler Proxy
*loClient.Proxy = loBridge.CreateInstance("System.Net.WebProxy","http://127.0.0.1:8888",.F.)
loCallback = CREATEOBJECT("HttpCallback")
*** HTTP GET
*** execute and returns immediately
loTask = loBridge.InvokeTaskMethodAsync(loCallback, loClient,"DownloadStringTaskAsync","https://west-wind.com/wconnect/TestPage.wwd")
? loTask && object
? "Mainline Done..."
*** HTTP POST
loClient2 = loBridge.CreateInstance("System.Net.WebClient")
lcPost = "LastName=Strahl&FirstName=Rick&Company=West+Wind+Technologies"
loBridge.InvokeMethod(loClient2,"Headers.Add","Content-Type","application/x-www-form-urlencoded")
? loBridge.cErrORMSG
loTask = loBridge.InvokeTaskMethodAsync(loCallback, loClient2,"UploadDataTaskAsync","https://west-wind.com/wconnect/TestPage.wwd",CAST(lcPost as BLOB))
? "Mainline Done..."
RETURN
DEFINE CLASS HttpCallback as AsyncCallbackEvents
*** Returns the result of the method and the name of the method name
FUNCTION OnCompleted(lvResult,lcMethod)
? "File received. Size: " + TRANSFORM(LEN(lvResult))
? SUBSTR(lvResult,1,1000)
*** Convert binary data to string (optional)
IF VARTYPE(lvResult) = "Q"
lvResult = CAST( lvResult as M)
ENDIF
*ShowHtml(lvResult)
ENDFUNC
* Returns an error message, a .NET Exception and the method name
FUNCTION OnError(lcMessage,loException,lcMethod)
? "Error: " + lcMethod,lcMessage
ENDFUNC
ENDDEFINE
See also:
Class wwDotNetBridge© West Wind Technologies, 1996-2024 • Updated: 08/18/24
Comment or report problem with topic