# A guide to the use of asynchronous interfaces

Header file:wmpf / operation.h.

The interface of the SDK has a lot of asynchronous operations.If you see a function with a return value typewx_operation_t,This means that this function is an asynchronous function and needs to call any of the following functions at the right time to wait for the results to be executed asynchronously:

  • Wx_operation_wait (): synchronous blocking waiting.Support setting timeouts.
  • Wx_operation_await (): Waits asynchronously, does not block the current thread.Support for setting callback functions.

If you do not care about the result of the asynchronous operation, you can call the following function to release thewx_operation_tobject.

  • Wx_operation_destroy (): Actively destroyswx_operation_tobjects.The asynchronous operation will continue to execute.

If you want to cancel an asynchronous operation, you can call the following function:

  • Wx_operation_cancel (): Cancel asynchronous operation.

Pay special attention to the life cycle of thewx_operation_tobject to avoid internal leakage:

  • Callwx_operation_waitorwx_operation_awaitfunction until the end of the asynchronous call, and then thewxf_operation_tobject is automatically recycled.
  • If you callwx_operation_waitOvertime, you can call thewx_operation_waitfunction to continue the wait, or call thewxf_operation_cancel (]]to actively terminate the asynchronous call.Cancelwx_operation_tThe object will also be automatically recycled.
  • You can also manually callwx_operation_destroy ()The operation object is destroyed, in which case the asynchronous operation continues, but the result of the asynchronous call is not received.

# 1. Wait in synchrony

Synchronously block the current thread until the operation is complete or timeout.

  • If the operation completes (including normal returns and interface exceptions),wx_operation_tis automatically released.
  • ReturnsWXERROR_TIMEOUTif the asynchronous operation is not completed within the specified time.There are the following options:
    • Reinvokewx_operation_wait ()to continue waiting;
    • Callwx_operation_cancel ()to cancel an asynchronous call;
    • Callwx_operation_destroy ()to destroy the operation object.
wx_error_t wx_operation_wait(wx_operation_t operation, uint32_t timeout_ms);

parameter

attribute type Introductions
operation wx_operation_t The operation object you want to wait for
timeout_ms uint32_t The timeout time, per millisecond.Setting to0indicates no timeout.

Return value

Results for wx_error_twait

Error code Introductions
WXERROR_OK The interface call completes normally and the operation object is automatically released.
WXERROR_TIMEOUT Operation exceeded time limit.
Other The interface invocation exception returns an error and the operation object is automatically released.

# 2. Asynchronous Waiting

Waiting asynchronously does not block the current thread.The result of the asynchronous operation is notified by thecallbackfunction.wx_operation_tThe object will be automatically released.

void wx_operation_await(wx_operation_t operation,
                        wx_operation_callback_t callback,
                        void* user_data);

parameter

attribute type Introductions
operation wx_operation_t The operation object you want to wait for
callback wx_operation_callback_t callback
user_data void* Custom data you want to receive in a callback function.

# 3. Cancel asynchronous operations

Cancel the asynchronous operation and thewx_operation_tobject is released.After calling this function, no operation should be performed on thewx_operation_tobject.

wx_error_t wx_operation_cancel(wx_operation_t operation);

parameter

attribute type Introductions
operation wx_operation_t The operation object you want to cancel

Return value

Results for wx_error_tcancel

# 4. Release the object

Actively releases thewx_operation_tobject, but does not cancel the asynchronous operation itself.After calling this function, no operations should be performed on the operation object.

  • If you don't call the wait or cancel function on thewx_operation_tobject and don't care about the results of the asynchronous operation, you can call this function to manually release thewxf_operation_sobject.
  • When callingwx_operation_wait ()function returnsWXERROR_TIMEOUT,If you do not want to continue waiting, you can call this function to release thewx_operation_tobject.
  • In other cases thewx_operation_tobject is automatically released and should not be called to actively release it.
void wx_operation_destroy(wx_operation_t operation);

parameter

attribute type Introductions
operation wx_operation_t The operation object you want to release