Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 没有UI线程方法:InvalidOperationException_Wpf_Multithreading_Asynchronous_Dispatcher_Invalidoperationexception - Fatal编程技术网

Wpf 没有UI线程方法:InvalidOperationException

Wpf 没有UI线程方法:InvalidOperationException,wpf,multithreading,asynchronous,dispatcher,invalidoperationexception,Wpf,Multithreading,Asynchronous,Dispatcher,Invalidoperationexception,我在WPF项目中的UI有问题 概念: C#中的两个客户端通过与Linux上的Java服务器聊天进行通信 对于其中一个客户端,我可以用“/kick[user][reason]”踢另一个聊天。对于从发送方接收套接字到被踢出的用户,我处于异步回调中,因此我很少在UI线程中工作。因此对于解决无UI方法问题,我使用Dispatcher。因此,对于关闭当前UI,我需要 this.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate { t

我在WPF项目中的UI有问题

概念:

C#中的两个客户端通过与Linux上的Java服务器聊天进行通信

对于其中一个客户端,我可以用“/kick[user][reason]”踢另一个聊天。对于从发送方接收套接字到被踢出的用户,我处于异步回调中,因此我很少在UI线程中工作。因此对于解决无UI方法问题,我使用Dispatcher。因此,对于关闭当前UI,我需要

this.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate { this.Close(); });
它工作得很好,但是现在我需要调用另一个UI(用户的连接菜单),但是当我使用“Show()”方法时,我得到了

   'System.InvalidOperationException' in PresentationCore.dll
   at System.Windows.Input.InputManager..ctor()
   at System.Windows.Input.InputManager.GetCurrentInputManagerImpl()
   at System.Windows.Input.KeyboardNavigation..ctor()
   at System.Windows.FrameworkElement.FrameworkServices..ctor()
   at System.Windows.FrameworkElement.EnsureFrameworkServices()
   at System.Windows.FrameworkElement..ctor()
   at System.Windows.Controls.Control..ctor()
   at System.Windows.Window..ctor()
所以我也试着把它放在调度程序里

 MainWindow mw = new MainWindow();
 mw.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate { mw.Show(); });
但是我得到了相同的错误(System.invalidooperationexception)

     this.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate
     {
         MainWindow mw = new MainWindow();
         mw.call();
         this.Close();
     });
怎么做

方法:

数据包接收:(不是很有用,但也许我可以用另一种方式…?)

    private void callBack(IAsyncResult aResult)
    {
        String message = "";
        try
        {
            int size = sck.EndReceiveFrom(aResult, ref ip);
            if (size > 0)
            {
                byte[] receive = new byte[1024];
                receive = (byte[])aResult.AsyncState;
                message = Encoding.Default.GetString(receive, 0, 1024);
                //class for execute sockets informations that the server sends
                new Event(message, this);
            }
            byte[] buffer = new byte[1024];
            //restart async task
            sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ip, new AsyncCallback(callBack), buffer);
        }
        catch (Exception) { }
    }
用户:


是的,我发现了问题

我只需要创建一个在新的主UI线程中显示窗口的方法,然后我只需要在关闭旧UI的代理中调用这个方法

     this.Dispatcher.BeginInvoke((System.Threading.ThreadStart)delegate
     {
         MainWindow mw = new MainWindow();
         mw.call();
         this.Close();
     });
这里是MainWindow类中的调用方法(新UI主线程)

    public void call()
    {
        this.Show();
        MessageBox.Show("You get kicked by \"" + "sender" + "\" for: " + "reason", "Server: /kick");
    }

您可能会重复获得异常,原因与任何人都一样:您试图访问线程中的对象,而不是拥有该对象的线程。请参阅建议的重复。在这种情况下,您是在后台线程中创建窗口(因此后台线程拥有该窗口)然后尝试在主UI线程中显示它。您还需要在主UI线程中创建窗口。