wwDotNetBridge::InvokeMethodAsync

Allows calling a .NET method and executing it asynchronously on a background thread. This is useful for long running operations that take a long time to complete, or if you need to run many simultaneous operations at the same time.

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

When making many callbacks, there are two ways you can pass your AsyncCallback object to this method:

  • Single shared Instance
    Using a single instance is more memory efficient as it uses a single reference to receive all callbacks. Use this if you don't care about state for the request and you are only interested in the actual result value without any previous association.

  • New instance for each callback
    Using a new instance for each callback allows you to attach state to the request being performed so you can trace the origin back to its source when the callback returns. To attach state you can add properties to the callback object that identify the request when you start the async method call which can then be picked up when the call returns. You can assign a pk, or a timestamp or whatever else you need to track across the async method call. IOW, you use the the event object as a store for some state data that you later need to reference.

Multi-threading Callbacks

Callbacks are fired from another thread and are essentially injected into the FoxPro execution pipeline and fire in the middle of existing code execution. This means don't make assumptions about the state of the environment when your code is called other than what you are directly accessing in your callback method and object. There are no guarantees about what's open and in scope when your callback method is fired.

Don't run a lot of code in these methods and most importantly don't change any external state that might affect the code that's being interrupted, as calling code may depend on things being set a certain way. IOW, don't change the workarea or close tables, or release objects etc. If you do run code and make environment changes, make absolutely sure you clean up and reset the environment exactly as when the call was initiated or you may cause crazy errors in the code that was interrupted by the callback.

Example

CLEAR
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")

*** These methods return immediately - then fire events on loCallback when done
loBridge.InvokeMethodAsync(loCallback,loTests,"SlowHelloWorld","Rick")
loBridge.InvokeMethodAsync(loCallback,loTests,"SlowHelloWorld","Markus")

RETURN

*** Implement a subclass of AsyncCallbackEvents
*** Implement the OnCompleted() and OnError() methods
*** These methods are called on completion
*** Minimize state changes here and make them quick
*** the code in these method fires in the middle of
*** other FoxPro code.
DEFINE CLASS MyCallbacks as AsyncCallbackEvents

FUNCTION OnCompleted(lvResult,lcMethod)
? "Success: " + lcMethod,lvResult
ENDFUNC

FUNCTION OnError(lcMessage,loException,lcMethod)
? "Error: " + lcMethod,lcMessage
ENDFUNC

ENDDEFINE

See also:

Class wwDotNetBridge | wwDotNetBridge::InvokeStaticMethodAsync

© West Wind Technologies, 1996-2018 • Updated: 05/17/16
Comment or report problem with topic