Windows runtime 使用互斥时从未同步的块调用了对象同步方法

Windows runtime 使用互斥时从未同步的块调用了对象同步方法,windows-runtime,mutex,windows-8.1,windows-phone-8.1,Windows Runtime,Mutex,Windows 8.1,Windows Phone 8.1,我正在尝试使用后台任务创建Windows通用应用程序 我正在尝试编写一个在传入蓝牙连接时触发的后台任务。 阻止前台和后台创建连接。我试图在前台和后台实现相同的互斥 当我从蓝牙设备读取数据时,我发现以下错误 从未同步的块调用了对象同步方法 令我惊讶的是,这种错误偶尔会出现。我错过什么了吗 这是我的密码: public sealed class RFBackgroundTask : IBackgroundTask { .... Variable declarations p

我正在尝试使用后台任务创建Windows通用应用程序

我正在尝试编写一个在传入蓝牙连接时触发的后台任务。 阻止前台和后台创建连接。我试图在前台和后台实现相同的互斥

当我从蓝牙设备读取数据时,我发现以下错误

从未同步的块调用了对象同步方法

令我惊讶的是,这种错误偶尔会出现。我错过什么了吗

这是我的密码:

public sealed class RFBackgroundTask : IBackgroundTask
{

      .... Variable declarations 

    public async void Run(IBackgroundTaskInstance taskInstance)
    {

        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
        try
        {
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
            Debug.WriteLine("RFComm Task Running");
            hotwatchConnection = taskInstance.TriggerDetails as RfcommConnectionTriggerDetails;
            socket = hotwatchConnection.Socket;
            reader = new DataReader(socket.InputStream);
            // n = new Notification(hotwatchConnection.RemoteDevice.HostName);
            await Read();
        }
        catch (System.Exception e)
        {
            Debug.WriteLine("RFComm Task Error: {0}", e.Message);
            if (ismutexReleased == false)
            {
                Debug.WriteLine("Releaseing mutex because of error {0}:", e.Message);
                connectionMutex.ReleaseMutex();
                ismutexReleased = true;
            }
        }
        finally
        {
            if (ismutexReleased == false)
            {
                Debug.WriteLine("Releasing Mutex 2");
                connectionMutex.ReleaseMutex();
            }
            ismutexReleased = true;
        }
       deferral.Complete();

    }

    public IAsyncAction Read()
    {
        return Task.Run(async () =>
        {
            try
            {
                connectionMutex = new Mutex(false, CONNECTION_MUTEX_NAME);
                // Attempt to wait for the mutex
                var waitResult = connectionMutex.WaitOne();
                if (waitResult)
                {
                    Debug.WriteLine("Aquired Mutex Successfully");
                }
                // If the wait was not successful, fail the connect operation
                if (!waitResult) { throw new TimeoutException(); }

                if (reader != null)
                {
                    uint length = 500;
                    reader.InputStreamOptions = InputStreamOptions.Partial;
                    uint len = await reader.LoadAsync(length);
                    String message = reader.ReadString(len);
                    Debug.WriteLine("Read " + message + " In the First Attemnpt");
                    var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                    roamingSettings.Values["COMMAND"] = message;
                    //if(!message.StartsWith("01"))
                    //{
                    //    await ProcessCommand(message);
                    //}
                }
                reader.Dispose();
                socket.Dispose();
                socket = null;
                if (waitResult == true)
                    connectionMutex.ReleaseMutex();
                ismutexReleased = true;
                Debug.WriteLine("Released Mutex successfully after reading data");
            }
            catch (Exception e)
            {

                Debug.WriteLine(e.Message);
                if (ismutexReleased == false)
                {
                    Debug.WriteLine("Releaseing mutex because of error {0}:", e.Message);
                    connectionMutex.ReleaseMutex();
                    ismutexReleased = true;
                }
                throw;
            }
            finally
            {
                if (ismutexReleased == false)
                {
                    connectionMutex.ReleaseMutex();
                    Debug.WriteLine("Releasing Mutex");
                }
                ismutexReleased = true;
            }

        }).AsAsyncAction();


    }
   }

必须在获取互斥锁的同一线程上释放互斥锁。 当您等待异步方法返回到同一上下文时,不能保证返回到同一线程

改用信号灯