簡體   English   中英

C#WPF InvalidOperationException未處理

[英]c# WPF InvalidOperationException was unhandled

打開窗口並關閉另一個窗口時出現錯誤。

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;
        }
    }
}
}

基本上,當關閉“第一個表單”時,它給我“ Public Control()”錯誤。

嗯...我想打開另一個表格然后關閉另一個...基本上

編輯:更改類名稱...

回調ReadData可能在無法訪問UI線程的后台線程中被調用。 您將需要使用Dispatcher.BeginInvoke在此說明)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM