Partilhar via


CoreWebView2.TrySuspendAsync Method

Definition

An app may call this API to have the WebView2 consume less memory.

public System.Threading.Tasks.Task<bool> TrySuspendAsync ();
member this.TrySuspendAsync : unit -> System.Threading.Tasks.Task<bool>
Public Function TrySuspendAsync () As Task(Of Boolean)

Returns

Examples

bool isSuccessful = await _iWebView2.CoreWebView2.TrySuspendAsync();
MessageBox.Show(this,
    (isSuccessful) ? "TrySuspendAsync succeeded" : "TrySuspendAsync failed",
    "TrySuspendAsync");

Remarks

This is useful when a Win32 app becomes invisible, or when a Universal Windows Platform app is being suspended, during the suspended event handler before completing the suspended event.

The IsVisible property must be false when the API is called. Otherwise, the API throws COMException with error code of HRESULT_FROM_WIN32(ERROR_INVALID_STATE).

Suspending is similar to putting a tab to sleep in the Edge browser. Suspending pauses WebView script timers and animations, minimizes CPU usage for the associated browser renderer process and allows the operating system to reuse the memory that was used by the renderer process for other processes.

Note that Suspend is best effort and considered completed successfully once the request is sent to browser renderer process. If there is a running script, the script will continue to run and the renderer process will be suspended after that script is done.

See Sleeping Tabs FAQ for conditions that might prevent WebView from being suspended. In those situations, the result of the async task is false.

The WebView will be automatically resumed when it becomes visible. Therefore, the app normally does not have to call Resume() explicitly.

The app can call Resume() and then TrySuspendAsync() periodically for an invisible WebView so that the invisible WebView can sync up with latest data and the page ready to show fresh content when it becomes visible.

All WebView APIs can still be accessed when a WebView is suspended. Some APIs like Navigate will auto resume the WebView. To avoid unexpected auto resume, check IsSuspended property before calling APIs that might change WebView state.

Applies to