异步方法中的UWP System.Runtime.InteropServices.COMException

时间:2017-07-03 13:02:45

标签: c# uwp async-await

我尝试修改Windows universal samples的代码以获取BLE广告并获得System.Runtime.InteropServices.COMException

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            if (!connected)
            {

                StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
                commandFile = await storageFolder.CreateFileAsync("command_to.txt", CreationCollisionOption.OpenIfExists);
                resultFile = await storageFolder.CreateFileAsync("result_from.txt", CreationCollisionOption.OpenIfExists);

                BasicProperties fileProperties = await commandFile.GetBasicPropertiesAsync(); //Here I get the COMException
                commandFileVersion = fileProperties.DateModified;

            }
        }

每次获得BLE广告时,此方法都会触发。几乎每次它都正常工作,但有时我会在COMException上获得GetBasicPropertiesAsync()

整个异常消息:

-       $exception  {System.Runtime.InteropServices.COMException (0x8000000E): A method was called at an unexpected time.

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at g831ble.MainPage.<OnAdvertisementReceived>d__10.MoveNext()}   System.Runtime.InteropServices.COMException

0 个答案:

没有答案