This useful method allows you to turn any .NET Method you call into an asynchronously called method that returns a callback to you on completion rather than an immediate result.
Use this method to turn long running methods into async callbacks that return only when the method completes while your mainline code can continue to run. Great for offloading HTTP requests, and downloads while other code can continue to run.
Uses the AsyncCallbackEvents class to receive notifications when requests complete or fail (see example).
o.InvokeMethodAsync(loCallbackEvents, loInstance, lcMethod, lvParm1-10)
Return Value
nothing
Parameters
loCallbackEvents
A callback events object that must have the following methods that fire when the respective events are fired:
- OnCompleted(lvResult, lcMethod)
- OnError(lcErrorMessage, loException, lcMethod)
loInstance
The .NET object instance on which to call the specified method
lcMethod
The method to call.
lvParm1 - lvParm10
Up to 10 parameters that can be passed to the method.
Remarks
Callback handlers are called back from a non-UI .NET thread and then marshalled back to the FoxPro UI thread. These callbacks can occur at any time after a program statement has executed on the FoxPro thread. This means the callback can interrupt a continuous block of currently executing code.
For this reason you should make sure to:
- Keep the callback handler code very short and fast
- Don't change state in the callback
- Restore the environment (aliases, settings etc)
- If necessary defer result processing by capturing
the result state and processing it later.
Example
Firing off the async request:
loBridge = CreateObject("wwDotNetBridge","V4")
loTests = loBridge.CreateInstance("Westwind.WebConnection.TypePassingTests")
*** IMPORTANT: The callback object must remain in scope
*** Either use a public var, or attach to an object that
*** stays in scope for the duration of the callback
PUBLIC loCallback
loCallback = CREATEOBJECT("MyCallbacks")
*** This methods returns immediately - then fire events when done
loBridge.InvokeMethodAsync(loCallback,loTests,"HelloWorld","Rick")
RETURN
Handling the Callback from the Async call:
*** Handle result values in this object
*** The callback object is called back
*** when the method completes or fails
DEFINE CLASS MyCallback as AsyncCallbackEvents
*** Returns the result of the method and the name of the method name
FUNCTION OnCompleted(lvResult,lcMethod)
? "Success: " + lcMethod,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 | wwDotNetBridge::InvokeStaticMethodAsync | Class AsyncCallbackEvents© West Wind Technologies, 1996-2024 • Updated: 03/11/19
Comment or report problem with topic