-1

打开一个窗口并关闭另一个窗口时出现错误。

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

我正在使用 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;
        }
    }
}
}

基本上,当关闭第一个表单时,它给了我“公共控制()”的错误......

嗯...我想打开另一个表格并关闭另一个...基本上

编辑:更改类名...

4

1 回答 1

2

回调ReadData可能是在无法访问 UI 线程的后台线程中调用的。您将需要使用Dispatcher.BeginInvoke在此处解释)。

于 2014-02-08T17:47:59.407 回答