在异步作废Dowork事件之前触发RunWorkerCompleted [英] RunWorkerCompleted fired before async void Dowork event

查看:227
本文介绍了在异步作废Dowork事件之前触发RunWorkerCompleted的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用后台工作人员进行BLE RSSI级别测试.

I'm using A background worker for BLE RSSI level test.

我的问题是RunWorkerCompleted事件在DoWork完成操作之前立即被触发.

my problem is that RunWorkerCompleted event is fired immediately, way before DoWork done it's operation.

大多数DoWork事件操作是创建广告监视程序,并等待来自低功耗蓝牙设备的信号. 信号电平将从主线程更新,结果的处理将在后台工作程序中进行.

most of the DoWork event operation is to create an advertisement watcher and wait for Signal from A Bluetooth low energy device. the signal level will be updated fromthe main thread and handling of the result will be on the background worker.

这是我打电话给后台工作人员的时间:

here is when I call the Background worker:

 ...
  worker = new BackgroundWorker();
        worker.DoWork += callTestBLE;
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.RunWorkerAsync(RSSI_Label);
  }

事件处理程序:

private async void callTestBLE(object sender, DoWorkEventArgs e)
    {
            BluetoothLEAdvertisementWatcher watcher1 ;
            BluetoothLEAdvertisementFilter advertisementFilter1;
            int rssiRetries1 = RSSIRETRIES;
            RssiValue = "";

            advertisementFilter1 = new BluetoothLEAdvertisementFilter();
            try
            {
                advertisementFilter1.Advertisement.LocalName = myUswm.getAdvetrismentName();
                checkRSSI = true;
            }
            catch (Exception) { checkRSSI = false; return; }

            watcher1 = new BluetoothLEAdvertisementWatcher(advertisementFilter);
            watcher1.ScanningMode = BluetoothLEScanningMode.Active;
            watcher1.Received += OnAdvertisementReceived;
            // Wait 5 seconds to make sure the device is really out of range
            watcher1.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(5000);
            watcher1.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000);

            try
            {
                watcher1.Start(); 
                await testBLEAsync();
                if (myUswm.getConnectionStatus() == DISCONNECTED)
                {
                    checkNextUUTClick(new object(), new RoutedEventArgs()); return;
                }
                for (int i = 0; i < 5; i++)
                {
                    // if (RssiSamplesNum <= 0 || --rssiRetries < 0)
                    if (RssiSamplesNum <= 0 || --rssiRetries1 < 0)
                    {
                        //serviceList.Clear();
                        watcher1.Stop();
                        rssiRetries1 = RSSIRETRIES;
                        RssiSamplesNum1 = numOfAdvertismentSamples;
                        break;
                    }
                    else
                    {
                       ((Label)e.Argument).Content = RssiValue;
                        /*RSSI_Label.Dispatcher.Invoke(new Action(() =>
                        { RSSI_Label.Content = RssiValue; }));*/
                    }
                    Thread.Sleep(2000);
                  }    
            }
    catch (Exception err) { }
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
       finalizeBleTest();
    }

感谢您的帮助!

推荐答案

asyncawait出现的问题. BackgroundWorker有点过时了,不支持异步代码.因此,当您等待testBLEAsync调用时,callTestBLE方法将完成,并且此时您将调用RunWorkerCompleted事件,而实际代码将继续在后台运行.

The problem here with the async and await. BackgroundWorker is a bit outdated and do not support asynchronous code. So when you await for testBLEAsync call, callTestBLE method finishes and at that moment you have your RunWorkerCompleted event called, while actual code continue to work in the background.

最简单的解决方案是将代码中的async/await完全删除,一切都将按预期工作,或者,您也可以使用任务和任务延续来重写代码.

So simplest solution is to completely remove async/await from your code and everything should work as expected, or, alternatively, you can rewrite your code using tasks and task continuations.

这篇关于在异步作废Dowork事件之前触发RunWorkerCompleted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆