如何从异步方法回调更新c#wpf中的gui

如何从异步方法回调更新c#wpf中的gui,c#,.net,wpf,asynchronous,mvvm-light,C#,.net,Wpf,Asynchronous,Mvvm Light,我在stackoverflow和net上搜索过,但我找不到解决问题的方法 我以异步方式读取流。我想要回拨来更新gui [STAThread] private void ClientLoggedCallback(IAsyncResult res) { try { MailClient.Helpers.Client.getInstance().client.GetStream().EndWrite(res);

我在stackoverflow和net上搜索过,但我找不到解决问题的方法

我以异步方式读取流。我想要回拨来更新gui

[STAThread]
    private void ClientLoggedCallback(IAsyncResult res)
    {
        try
        {
            MailClient.Helpers.Client.getInstance().client.GetStream().EndWrite(res);
            MailClient.Helpers.Client.getInstance().asyncRecieveEncryptedProtocolMessage(new AsyncCallback(LoginInfo_recieved));
        }
        catch { }
    }
    [STAThread]
    private void LoginInfo_recieved(IAsyncResult res)
    {
        try
        {
            MailClient.Helpers.Client.getInstance().client.GetStream().EndRead(res);
            MailClient.AsyncState state = (MailClient.AsyncState)res.AsyncState;
            string answer = Aes.DecryptStringFromBytes_Aes(state.buffer, state.AES_KEY, state.AES_IV);
            if (answer.Contains("OK"))
            {
                string[] answer_params = answer.Split(',');
                LoggedUserInfo.USER_ID = Convert.ToInt32(answer_params[1]);
                LoggedUserInfo.USER_LOGIN = answer_params[2];

                //zalogowano
                //this.TargetWindow = new MessageListsWindow();
                Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() => this.TargetWindow = new MessageListsWindow()));
                //System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,  new Action(() => this.TargetWindow = new MessageListsWindow()));
            }
            else
            {
                //zle dane
                System.Windows.MessageBox.Show("Zle dane");
            }
        }
        catch(Exception exep) { }
    }
这是asyncSendEncryptedProtocolMessage的声明

asyncSendEncryptedProtocolMessage(string message, AsyncCallback callBack) 
使用函数

clientStream.BeginWrite(encryptedMessage, 0, encryptedMessage.Length, callBack, st);
当代码执行时,我得到异常“调用线程必须是STA,因为许多UI组件都需要它。”我读过关于“SetApartmentState(ApartmentState.STA);”的文章,但我不知道如何将其应用于回调。我也尝试过使用STAThread属性,但它不起作用。我使用MVVM轻型框架

堆栈跟踪

" w System.Windows.Threading.DispatcherObject.VerifyAccess()\r\n   w System.Windows.Application.get_MainWindow()\r\n   w MailClient.ViewModel.MainWindowModel.LoginInfo_recieved(IAsyncResult res) w c:\\Users\\oem\\Documents\\Visual Studio 2012\\Projects\\MvvmLight3\\MailClient\\ViewModel\\MainWindowModel.cs:wiersz 171"
然后像这样使用它:

MailClient.Helpers.Client.getInstance()
.asyncRecieveEncryptedProtocolMessage(new AsyncCallback(()=> 
Application.Current.MainWindow.Dispatch(LoginInfo_recieved)));

你在干什么?它不是指向MessageBox.ShowCall吗?我不关注MessageBox,可能它会给出相同的错误。我将使用stacktrace进行编辑。我应该将此Dispatch()方法放在何处?在任何publis静态类中,它是一个扩展方法。此代码为worng。当我将委托AsyncCallback更改为MailClient.Helpers.Client.getInstance()时,它有一个参数是IAsyncResult(新的AsyncCallback((a)=>System.Windows.Application.Current.MainWindow.Dispatch(LoginInfo_received));它也不起作用
MailClient.Helpers.Client.getInstance()
.asyncRecieveEncryptedProtocolMessage(new AsyncCallback(()=> 
Application.Current.MainWindow.Dispatch(LoginInfo_recieved)));