Calling Async or Task returning Methods

.NET supports async code that can be called with async and await. Behind the scenes .NET uses a state machine for these functions that access methods that actually a return an object of type Task or Task<T>.

In .NET you can make async calls like this:

public async Task<string> MakeHttpCall(string url)
    var client = new WebClient();
    string http  = await client.DownloadStringTaskAsync(url);
    return http;

which is asynchronous and depends on the .NET compiler magic.

We can't do this same exact code in FoxPro because FoxPro doesn't support the asynchronous pattern used by .NET, but we can use the actual underlying Task API to force the async call to be run synchronously:

public string MakeHttpCall(string url)
    var client = new WebClient();
    var task = client.DownloadStringTaskAsync(url); // returns immediately
    // waits until Result is available
    string http = task.Result;
    return http;

By calling the async method and capturing the task you are running the task in the background, but then calling .Result to return the result causes the app to wait for completion blocking the main thread.

The latter code can be simulated in FoxPro with the following:

loBridge = GetwwDotnetBridge()

loClient = loBridge.CreateInstance("System.Net.WebClient")

*** execute and returns immediately
loTask = loBridge.InvokeMethod(loClient,"DownloadStringTaskAsync","")
? loTask  && object

*** Waits for completion
lcHtml = loBridge.GetProperty(loTask,"Result")
? lcHtml

Note that you have to call InvokeMethod() rather than directly accessing the .Result object because the the result is of Task<string> in this case and generic types cannot be processed directly via COM and require the .NET proxy to convert the value for you.

.Result Warning

Microsoft doesn't recommend calling .Result or .Wait() or .WaitAll() on Task as it blocks the original UI thread and there is potential for an application to freeze while multiple tasks wait for completion and are blocked. Use this feature carefully.

For more info on how async works with wwDotnetBridge check out this blog post:

© West Wind Technologies, 1996-2024 • Updated: 10/05/18
Comment or report problem with topic