C# 无效的跨线程访问Windows Azure

C# 无效的跨线程访问Windows Azure,c#,multithreading,azure,windows-phone-8,azure-mobile-services,C#,Multithreading,Azure,Windows Phone 8,Azure Mobile Services,我正在使用Windows Azure移动服务开发Windows Phone 8应用程序 我有以下代码: private async void appBarButton1_Click(object sender, EventArgs e) { var table = App.MobileService.GetTable<ChatRoomOverview.chatrooms>(); //TODO List<ChatRoomO

我正在使用Windows Azure移动服务开发Windows Phone 8应用程序

我有以下代码:

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);
        }
    }
private async void appBarButton1\u单击(对象发送方,事件参数e)
{
var table=App.MobileService.GetTable();
//待办事项
列出聊天室=等待表。其中(chat=>chat.chatroomName==chatroomName.Text)。toListSync();
if(chattrooms.Count()<1)
{
ChatRoomOverview.chatrooms chatrooms=新建ChatRoomOverview.chatrooms{chatroomName=chatroomName.Text,Created=DateTime.ToString(),content=ChatRoomOverview.userName+“|::::::::;”,LastPost=FirstPost.Text+“::::::;”,tag1=tag1.Text,tag2=tag2.Text,tag3=tag3.Text,isPrivate=isPrivateCheck.IsChecked.Value,userName=ChatRoomOverview.userName,Popular=“0”};
等待表。插入同步(聊天室);
long tempID=ChatRoom.Id;
等待订阅文件室(临时ID);
NavigationService.Navigate(新Uri(“/ChatRoomOverview.xaml”,UriKind.Relative));
}
其他的
{
Show(“此聊天室已准备就绪,请转到搜索并签出:),“聊天室已存在!”,MessageBoxButton.OK);
}
}
上面的代码是在用户想要创建聊天室时执行的。这里一切正常。但在我新的应用程序迭代中,我想实现pushnotification,因此我想使用subscription表。这在SubscribeTonewchatRoom函数中介绍。它的变量是在插入聊天室项目时给出的聊天室的已发布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;
    }
private System.Threading.Tasks.Task subscribetOneWChatRoom(长tempID)
{
ChatRoomOverview.Subscription Subscription=newchatroomoverview.Subscription{ContentID=Convert.ToInt32(tempID),userId=App.userinfomation};
**错误**App.MobileService.GetTable().InsertAsync(订阅);
返回null;
}
然后执行上述代码以插入订阅。这段代码在代码中的其他地方也可以使用,所以我不明白错误是什么。但是错误发生在我在代码中写入错误的地方。问题的底部是一个完全例外

所以我不明白为什么我得到了无效的跨线程访问

额外的 好的,根据Jaishind的回答和评论,我想出了下面的代码。我现在可以看到,与我交互的表得到了正确的数据。但我还是有例外

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);
    }
private async void bwSubscription\u DoWork(对象发送方,DoWorkEventArgs e)
{
List genericlist=e.参数为List;
var table=wait App.MobileService.GetTable();
//列表聊天室=表.Where(chat=>chat.chatroomName==chatroomName.Text)
//表.InsertAsync(聊天室);
ChatRoomOverview.chatrooms chat=(ChatRoomOverview.chatrooms)常规列表[0];
//var ChatRoom=table.Where(chatting=>chatting.chatroomName==chat.chatroomName.toListSync();
long tempID=chat.Id;
ChatRoomOverview.Subscription订阅=新建ChatRoomOverview.Subscription{ContentID=tempID,userId=App.userinfomation};
等待App.MobileService.GetTable().InsertAsync(订阅);
}
但是在插入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;
            }
        }

    }
private async void bwSubscription\u DoWork(对象发送方,DoWorkEventArgs e)
{
bool subscribed=false;
List genericlist=e.参数为List;
var table=App.MobileService.GetTable();
//列表聊天室=表.Where(chat=>chat.chatroomName==chatroomName.Text)
//表.InsertAsync(聊天室);
ChatRoomOverview.chatrooms chat=(ChatRoomOverview.chatrooms)常规列表[0];
//var ChatRoom=table.Where(chatting=>chatting.chatroomName==chat.chatroomName.toListSync();
long tempID=chat.Id;
而(!订阅)
{
尝试
{
ChatRoomOverview.Subscription订阅=新建ChatRoomOverview.Subscription{ContentID=tempID,userId=App.UserInfoomatiod,Notifications=0};
等待App.MobileService.GetTable().InsertAsync(订阅);
}
抓住
{
}
List subscript=await App.MobileService.GetTable()。其中(subs=>subs.ContentID==tempID&&subs.userId==App.userinforomationId&&subs.Notifications==0)。ToListSync();
如果(下标计数>0)
{
ChatRoomOverview.userName=“”;
下标=真;
}
其他的
{
下标=假;
}
}
}
我的全部例外情况

  • $exception{System.UnauthorizedAccessException:无效的跨线程访问。 在MS.Internal.XcpImports.CheckThread()处 位于MS.Internal.XcpImports.MessageBox\u ShowCore(字符串MessageBox文本、字符串标题、UInt32类型) 在System.Windows.MessageBox.ShowCore(字符串messageBoxText、字符串标题、MessageBoxButton按钮) 在System.Windows.MessageBox.Show(字符串messageBoxText) 在ChatRoom.App.CurrentChannel_ShellToastNotificationReceived(对象发送方,NotificationEventArgs e) 在Microsoft.Phone.Notification.ShellObjectChannelInternals.OnNotificationReceived(IntPtr blob,UInt32 blobSize)中 位于Microsoft.Phone.Notification.ShellObjectChannelInternals.ChannelHandler(UInt32事件类型、IntPtr blob1、UInt32 blobSize1、IntPtr blob2、UInt32 blobSize2) 位于Microsoft.Phone.Notification.HttpNotificationChannel.Dispatch(对象线程上下文) 位于System.Threading.QueueUserWorkItemCallback.WaitCallback_上下文(对象状态) 位于System.Threading.ExecutionContext.RunInternal(ExecutionContext exe
    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;
                }
    
    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;
            }
        }
    
    }