0

我正在開發使用Windows Azure移動服務的Windows Phone 8應用程序。無效的跨線程訪問Windows Azure

我有以下代碼:

private async void appBarButton1_Click(object sender, EventArgs e) 
    { 
     var table = App.MobileService.GetTable<ChatRoomOverview.chatrooms>(); 
     //TODO 
     List<ChatRoomOverview.chatrooms> ChatRooms = await table.Where(chat => chat.chatroomName == ChatroomName.Text).ToListAsync(); 
     if (ChatRooms.Count() < 1) 
     { 
      ChatRoomOverview.chatrooms ChatRoom = new ChatRoomOverview.chatrooms { chatroomName = ChatroomName.Text, Created = DateTime.Today.Date.ToString(), content = ChatRoomOverview.userName + "|:::|" + FirstPost.Text + "|::|::|", LastPost = FirstPost.Text + "|::|::|", tag1 = tag1.Text, tag2 = tag2.Text, tag3 = tag3.Text, isPrivate = isPrivateCheck.IsChecked.Value, userName = ChatRoomOverview.userName, Popular = "0" }; 

      await table.InsertAsync(ChatRoom); 
      long tempID =ChatRoom.Id; 
      await subscripeToNewChatRoom(tempID); 

      NavigationService.Navigate(new Uri("/ChatRoomOverview.xaml", UriKind.Relative)); 
     } 
     else 
     { 
      MessageBox.Show("This Chatroom is allready created go to Search and check it out :)", "Chatroom Exists!", MessageBoxButton.OK); 
     } 
    } 

當用戶想要創建一個聊天室上面的代碼excuted。一切工作在這裏。但在我的應用程序的新迭代中,我想實現pushnotification,因此我想使用一個subsription表。這是在subscripeToNewChatRoom函數中引入的。它的變量是插入聊天室項目時給出的聊天室ID。這個值是存在的並且是正確的。

private System.Threading.Tasks.Task subscripeToNewChatRoom(long tempID) 
    { 
     ChatRoomOverview.Subscription Subscription = new ChatRoomOverview.Subscription { ContentID = Convert.ToInt32(tempID), userId = App.UserInfromationID }; 
     **ERROR** App.MobileService.GetTable<ChatRoomOverview.Subscription>().InsertAsync(Subscription); 
     return null; 
    } 

上面的代碼然後被執行插入訂閱。此代碼在代碼中的其他地方工作,所以我不明白錯誤是什麼。但是錯誤發生在代碼中寫入錯誤的地方。完整的例外是在問題的底部。

所以我不明白爲什麼我得到爲什麼我得到無效的跨線程訪問?

額外 好吧在Jaihind的答案和意見的基礎上,我想出了下面的代碼。現在我可以看到我與之交互的表格獲取正確的數據。 Buuut我仍然得到例外。

private async void bwSubscription_DoWork(object sender, DoWorkEventArgs e) 
    { 
     List<object> genericlist = e.Argument as List<object>; 

     var table = await App.MobileService.GetTable<ChatRoomOverview.chatrooms>(); 
     //List<ChatRoomOverview.chatrooms> ChatRooms = table.Where(chat => chat.chatroomName == ChatroomName.Text) 

     //table.InsertAsync(ChatRoom); 
     ChatRoomOverview.chatrooms chat = (ChatRoomOverview.chatrooms)genericlist[0]; 
     //var ChatRoom = table.Where(chatting => chatting.chatroomName == chat.chatroomName).ToListAsync(); 

     long tempID = chat.Id; 

     ChatRoomOverview.Subscription Subscription = new ChatRoomOverview.Subscription { ContentID = tempID, userId = App.UserInfromationID }; 
     await App.MobileService.GetTable<ChatRoomOverview.Subscription>().InsertAsync(Subscription); 
    } 

但是,當插入try/catch時,爲了處理沒有發生的異常,該項不會被插入到azure表中。無效的跨線程訪問:

工作液

private async void bwSubscription_DoWork(object sender, DoWorkEventArgs e) 
    { 
     bool subscriped = false; 
     List<object> genericlist = e.Argument as List<object>; 

     var table = App.MobileService.GetTable<ChatRoomOverview.chatrooms>(); 
     //List<ChatRoomOverview.chatrooms> ChatRooms = table.Where(chat => chat.chatroomName == ChatroomName.Text) 

     //table.InsertAsync(ChatRoom); 
     ChatRoomOverview.chatrooms chat = (ChatRoomOverview.chatrooms)genericlist[0]; 
     //var ChatRoom = table.Where(chatting => chatting.chatroomName == chat.chatroomName).ToListAsync(); 

     long tempID = chat.Id; 
     while (!subscriped) 
     { 
      try 
      { 
       ChatRoomOverview.Subscription Subscription = new ChatRoomOverview.Subscription { ContentID = tempID, userId = App.UserInfromationID , Notifications = 0}; 
       await App.MobileService.GetTable<ChatRoomOverview.Subscription>().InsertAsync(Subscription); 
      } 
      catch 
      { 

      } 
      List<ChatRoomOverview.Subscription> subscript = await App.MobileService.GetTable<ChatRoomOverview.Subscription>().Where(subs => subs.ContentID == tempID && subs.userId == App.UserInfromationID && subs.Notifications == 0).ToListAsync(); 
      if (subscript.Count > 0) 
      { 
       ChatRoomOverview.userName = ""; 
       subscriped = true; 
      } 
      else 
      { 
       subscriped = false; 
      } 
     } 

    } 

我完全

  • $ {異常異常System.UnauthorizedAccessException的。 在MS.Internal.XcpImports.CheckThread() 在MS.Internal.XcpImports.MessageBox_ShowCore(字符串messageBoxText,字符串標題,UInt32的類型) 在System.Windows.MessageBox.ShowCore(字符串messageBoxText,字符串標題,MessageBoxButton按鈕) 在System.Windows.MessageBox.Show(字符串messageBoxText) 在ChatRoom.App.CurrentChannel_ShellToastNotificationReceived(對象發件人,NotificationEventArgs E) 在Microsoft.Phone.Notification.ShellObjectChannelInternals.OnNotificationReceived(IntPtr的團塊,UInt32的blobSize) 在Microsoft.Phone。 Notification.ShellObjectChannelInternals.ChannelHandler(UInt32 eventType,IntPtr blob1,UInt32 blobSize1,IntPtr blob2,UInt32 blobSize2) at Microsoft.Phone.Notification.HttpNotificationChannel.Dispatch(Object thr eadContext) 在System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(對象狀態) 在System.Threading.ExecutionContext.RunInternal(的ExecutionContext的ExecutionContext,ContextCallback回調,對象的狀態,布爾preserveSyncCtx) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext, ContextCallback回調,對象的狀態,布爾preserveSyncCtx) 在System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在System.Threading.ThreadPoolWorkQueue.Dispatch() 在System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}系統.Exception {System。UnauthorizedAccessException}
+0

我不明白這與Azure有什麼關係。異常中的調用堆棧清楚地表明後臺線程已被要求顯示一個消息框。顯示消息框必須從GUI線程完成。 –

回答

0

工作液

private async void bwSubscription_DoWork(object sender, DoWorkEventArgs e) 
{ 
    bool subscriped = false; 
    List<object> genericlist = e.Argument as List<object>; 

    var table = App.MobileService.GetTable<ChatRoomOverview.chatrooms>(); 
    //List<ChatRoomOverview.chatrooms> ChatRooms = table.Where(chat => chat.chatroomName == ChatroomName.Text) 

    //table.InsertAsync(ChatRoom); 
    ChatRoomOverview.chatrooms chat = (ChatRoomOverview.chatrooms)genericlist[0]; 
    //var ChatRoom = table.Where(chatting => chatting.chatroomName == chat.chatroomName).ToListAsync(); 

    long tempID = chat.Id; 
    while (!subscriped) 
    { 
     try 
     { 
      ChatRoomOverview.Subscription Subscription = new ChatRoomOverview.Subscription { ContentID = tempID, userId = App.UserInfromationID , Notifications = 0}; 
      await App.MobileService.GetTable<ChatRoomOverview.Subscription>().InsertAsync(Subscription); 
     } 
     catch 
     { 

     } 
     List<ChatRoomOverview.Subscription> subscript = await App.MobileService.GetTable<ChatRoomOverview.Subscription>().Where(subs => subs.ContentID == tempID && subs.userId == App.UserInfromationID && subs.Notifications == 0).ToListAsync(); 
     if (subscript.Count > 0) 
     { 
      ChatRoomOverview.userName = ""; 
      subscriped = true; 
     } 
     else 
     { 
      subscriped = false; 
     } 
    } 

} 
0

願這能幫助你。

private subscripeToNewChatRoom(long tempID) 
      { 
       ChatRoomOverview.Subscription Subscription = new ChatRoomOverview.Subscription { ContentID = Convert.ToInt32(tempID), userId = App.UserInfromationID }; 
     Dispatcher.BeginInvoke(() => 
         { 
     App.MobileService.GetTable<ChatRoomOverview.Subscription>().InsertAsync(Subscription); 
         }); 

       return null; 
      } 
+0

不幸的是我仍然收到錯誤,我讀到了這個解決方案,但瞭解它是與UI有關的問題?我嘗試過,但沒有解決問題。 – JTIM

+0

@JTIM在appBarButton1_Click事件中使用Dispatcher。 – Jaihind

+0

我mremoved subscripeToNewChatRoom函數。並將代碼移動到appBarButton1_Click事件中,發生同樣的錯誤:/ – JTIM

0

不要試圖顯示從後臺線程的用戶界面元素。調用堆棧清楚地表明這是你正在嘗試做的事情。

QueueUserWorkItem意味着這是來自後臺線程的調用堆棧。然後我們看到MessageBox.Show()正在被調用。這會在桌面應用程序或應用程序中拋出相同的異常,並且不會與天青接觸。

相關問題