使用任务进行连续轮询

Ete*_*l21 5 c# asynchronous task polling task-parallel-library

这是我一直使用Threads/BackgroundWorker的东西,但我正在尝试迁移到Task的做事方式.

假设我有一个第三方SDK用于从USB端口读取字节.如果没有读取字节,则读取调用将阻塞并在100 ms后超时,返回null.如果读取字节,它会立即返回,返回读取字节的byte []数组.

所以我基本上需要一遍又一遍地轮询,并通过调用解析函数对接收到的字节采取措施.它是一个WPF应用程序,因此返回的字节应该能够传递给UI线程函数.

这样做的正确方法是什么?这是我到目前为止,它似乎工作,但我想确保它是使用TPL做事的正确方法:

private void _connectUsbButton_Click(object sender, RoutedEventArgs e)
{
   ListenForUsbMessagesAsync();
}

private async void ListenForUsbMessagesAsync()
{
    while (true)
    {
        byte[] readBytes = await ReadBytesAsync();
        Parse(readBytes);
    }
}

private Task<byte[]> ReadBytesAsync()
{
    Task<byte[]> readBytesTask = Task.Run(() =>
    {
        byte[] bytes;

        do
        {
            bytes = ReadBytes();
        } while (bytes == null);

        return bytes;
    });

    return readBytesTask;
}

private byte[] ReadBytes()
{
   byte[] readBytes = _usbSdk.ReadBytes(); //100ms timeout (returns null if no bytes read)
   return readBytes;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*SCP 6

对我来说没问题,这里只有一些建议:

private async Task ListenForUsbMessagesAsync(CancellationToken token)
{
    while (true)
    {
        byte[] readBytes = await ReadBytesAsync();
        Parse(readBytes);
        token.ThrowIfCancellationRequested();
    }
}
Run Code Online (Sandbox Code Playgroud)

在其他地方,比如在WPF Window.中存储这个

var tokenSource = new System.Threading.CancellationTokenSource();
Run Code Online (Sandbox Code Playgroud)

最后像这样调用你的函数

 private void _connectUsbButton_Click(object sender, RoutedEventArgs e)
 {
    ListenForUsbMessagesAsync(tokenSource.Token);
 }
Run Code Online (Sandbox Code Playgroud)

这样您就可以随时通过呼叫取消您的任务

tokenSource.Cancel()
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想使用"任务",则可以生成新的Thread并传入Dispatcher对象.通过这种方式,新创建的Thread可以安全地将内容解压到UI线程上.


bar*_*caf 5

由于您的轮询任务可能会运行很长时间,因此您应该考虑在专用线程中运行它.

您可以通过在创建轮询任务时传递TaskCreationOptions.LongRunning标志来实现此目的.

像这样:

Task<byte[]> readBytesTask = Task.Factory.StartNew(() =>
    {
        byte[] bytes;

        do
        {
            bytes = ReadBytes();
        } while (bytes == null);

        return bytes;
    }, TaskCreationOptions.LongRunning);
Run Code Online (Sandbox Code Playgroud)