Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 1.01 KB

CancellationToken.md

File metadata and controls

42 lines (37 loc) · 1.01 KB

正常写法Cancel()不会报错

CancellationTokenSource m_cts = new CancellationTokenSource();
public async UniTaskVoid XXX()
{
    await UniTask.Yield(cancellationToken:m_cts.Token);
}

public UniTaskVoid cancel()
{
    m_cts.Cancel();
    m_cts.DisPlay();
    m_cts = new CancellationTokenSource();
}

需要有返回值的写法。

CancellationTokenSource m_cts = new CancellationTokenSource();
public async UniTask<T> XXX()
{
    T t;
    await UniTask.Yield(cancellationToken:m_cts.Token);
    return t;
}

public async void Invoke_async()
{
   T t = await XXX().SuppressCancellationThrow();
}

public UniTaskVoid cancel()
{
    m_cts.Cancel();
    m_cts.DisPlay();
    m_cts = new CancellationTokenSource();
}

PS: 使用UniTaskVoid类型是不可await的~!