An error occurred in script '/home/yhr1ef23xez5/public_html/web2/app/model/bannerhit.php' on line 17:
Undefined index: HTTP_REFERER

Array
(
    [type] => post
    [name] => await
)

WinWrap® | Await Instruction/Function
logo WinWrap®
September 20, 2021

Await Instruction/Function

WinWrap® Basic scripts can await Async methods.

Use Await to call Async methods

  • Script events continue to fire.
  • The Async method's result is returned to the script when the method completes.
  • Script execution continues after the Async method completes.
  • Cancel Async methods when a script is stopped by passing the ScriptCancellationToken to Async methods.
  • Unlike VB.NET a WinWrap Basic method containing Awaits does not need to be an Async method.

Many .NET classes now have Async methods.

The Await function waits for the Async method to complete and returns the result. Script events can be handled and the UI is not blocked while Await is waiting for the Async method to complete.

The following sample script uses an HttpClient.GetAsync and HttpResponse.Content.ReadAsStringAsync to read a web page.

'#Reference #System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL '#Language "WWB.NET" Imports System.Net Imports System.Net.Http Sub Main Debug.Clear ' .NET 4 HttpClient needs this ServicePointManager.Expect100Continue = True ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 ' create a HttpClient Dim hc As New HttpClient ' get the robots.txt page Dim hrm As HttpResponseMessage = Await hc.GetAsync("https://www.winwrap.com/robots.txt", ScriptCancellationToken) ' read the page text Dim text As String = Await hrm.Content.ReadAsStringAsync() Debug.Print text End Sub

Await handles all the issues when dealing with Async methods. Unlike VB.NET, a WinWrap® Basic method containing Awaits does not need to be an Async method.

Await and Callbacks

When an Async method executes in a worker thread the callback defined by the script executes in the script's thread.

WinWrap Basic Thread Worker Thread
Script Mediator Mediator Async
Sub A
    ...
    Dim B As T1 =
        Await CAsync(AddressOf D,
            ScriptCancellationToken)
    ... DoEvents events
T1 CAsync(Func<...> callback, CancellationToken token)
{
    ...
    T2 E = callback.Invoke(args);
evh = new EventWaitHandle(...);
push pending call "D(args)"
evh.WaitOne()
pop pending call "D(args)" worker thread stopped
Function D(args) As T2
   ...
   Return Dresult
End Function
Dtemp = Dresult
evh.Set()
    ... DoEvents events // evh.WaitOne() completes
    E = Dtemp;
    ...
    return Cresult;
}
    B = Cresult
    ...
End Sub A

Copyright Polar Engineering, Inc.