新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

如何取消C#中的BeginInvoke操作,c#关闭程序

发布时间:2024-07-18 05:00:44

如何取消C#中的BeginInvoke操作

要取消C#中的BeginInvoke操作,可使用EndInvoke方法,并在调用BeginInvoke后立即调用EndInvoke方法。例如:

IAsyncResult result = this.BeginInvoke(new Action(() => { }));
this.EndInvoke(result);

另外,也能够使用CancellationToken来取消BeginInvoke操作。例如:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;

this.BeginInvoke(new Action(() => {
    if (token.IsCancellationRequested)
    {
        return;
    }

    // 履行需要取消的操作
}), token);

// 取消操作
cts.Cancel();

tiktok粉丝购买:https://www.smmfensi.com/