简体   繁体   English

C#WPF InvalidOperationException未处理

[英]c# WPF InvalidOperationException was unhandled

I have a error when opening a window and closing another. 打开窗口并关闭另一个窗口时出现错误。

The calling thread must be STA, because many UI components require this.

I am using RedCorona Sockets... http://www.redcorona.com/Sockets.cs Here is the code... 我正在使用RedCorona套接字... http://www.redcorona.com/Sockets.cs这是代码...

public partial class MainWindowThingy : Window
{
   public ClientInfo client;
    public MainWindowThingy() //The Error appears here
    {
        InitializeComponent();
        StartTCP();
    }
    public void StartTCP()
    {
        Socket sock = Sockets.CreateTCPSocket("localhost", 2345);
        client = new ClientInfo(sock, false); // Don't start receiving yet
        client.OnReadBytes += new ConnectionReadBytes(ReadData);
        client.BeginReceive();
    }
    void ReadData(ClientInfo ci, byte[] data, int len)
    {
        string msg = (System.Text.Encoding.UTF8.GetString(data, 0, len));
        string[] amsg = msg.Split(' ');
        switch (amsg[0])
        {
            case "login":
                if (bool.Parse(amsg[1]) == true)
                {
                    MainWindowThingy SecondWindow = new MainWindowThingy();
                    Login FirstWindow = new Login();
                    SecondWindow.Show();
                    FirstWindow.Close(); //It starts here, the error...
                }
                else
                {
                }
                break;
        }
    }
}
}

Basicly, It gives me the error at the "Public Control()" When closing The First Form... 基本上,当关闭“第一个表单”时,它给我“ Public Control()”错误。

Uhm... I want to open another form and close the other... basicly 嗯...我想打开另一个表格然后关闭另一个...基本上

Edit: Changed Class name... 编辑:更改类名称...

The callback ReadData is probably being called in a background thread which does not have access to the UI thread. 回调ReadData可能在无法访问UI线程的后台线程中被调用。 You will need to use Dispatcher.BeginInvoke (explained here ). 您将需要使用Dispatcher.BeginInvoke在此说明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM