简体   繁体   English

Task.Run取消

[英]Task.Run cancellation

before I update VS for mac I use 在我为Mac更新VS之前

using (var cancelSrc = new CancellationTokenSource())
                    {
                        using (UserDialogs.Instance.Loading("Connecting", cancelSrc.Cancel, "Cancel"))
                        {
                            await device.Connect().ToTask(cancelSrc.Token);
                        }
                    }

to connect a ble device 连接ble设备

after updated IDE comes up a error "operator '.' 更新后的IDE出现错误“ operator'”。 cannot be applied to operand of type void" 不能应用于void类型的操作数”

that I use 我用的

await Task.Run(() => 
                            {
                                cancelSrc.Token.ThrowIfCancellationRequested();
                                while (!cancelSrc.Token.IsCancellationRequested)
                                {
                                    device.Connect();
                                }
                            },cancelSrc.Token);

instead of await device.Connect().ToTask(cancelSrc.Token); 而不是await device.Connect().ToTask(cancelSrc.Token); but when I tapped on the loading screen in app it does nothing but keep connecting , and seems the connection will not finish forever. 但是当我在应用程序的加载屏幕中轻按时,它什么都没有做,而是继续保持连接,并且似乎连接不会永远结束。 please give me some help! 请给我一些帮助!

It seems that everyone is not interested in my question,I solve it by myself with some way looks not so smart. 似乎每个人都对我的问题不感兴趣,我以某种不太聪明的方式自己解决了这个问题。 Here's my solution:implement a Task method 这是我的解决方案:实现Task方法

public async Task ConnectDevice(IDevice device,CancellationTokenSource cancelSrc)
    {
        await Task.Run(() =>
        {
            device.Connect();
            while ((cancelSrc.Token.IsCancellationRequested==false)&&(device.IsConnected() == false))
            {

            }

        }, cancelSrc.Token);
    }

and when I want to connect I do like this: 当我想连接时,我会这样:

await ConnectDevice(device, cancelSrc);

this works for me 这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM