2014-02-08 1 views
-1

창을 열고 다른 창을 닫을 때 오류가 있습니다. 을 닫을 때 http://www.redcorona.com/Sockets.cs 여기 코드입니다 내가 RedCorona 소켓을 사용하고C# WPF InvalidOperationException 처리되지 않았습니다.

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

...

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는, 그것은 "공공 관리()" 에서 나에게 오류를 제공,536 : 첫 번째 양식 ...

음 ... 난

편집 basicly ... 또 다른 형태를 열고 기타를 닫으려면클래스 이름이 변경되었습니다 ...

+0

배경 스레드에서 호출 하시겠습니까? –

+0

창에 "제어"라는 이름을 지정하지 마십시오. .NET Framework의 클래스이므로 이미 문제를 일으킬 것입니다. – nvoigt

+0

나는 그렇게 생각한다. 그러나 나는 이걸로 디스패서를 사용하는 법을 모른다. 나에게 모범을 보일 수 있니? – user2993512

답변

2

콜백 ReadData은 UI 스레드에 액세스 할 수없는 백그라운드 스레드에서 호출되고있는 것 같습니다. Dispatcher.BeginInvoke (here 설명)을 사용해야합니다.

+0

당신은 나에게 모범을 줄 수 있냐? – user2993512

+0

내가 제공 한 링크를 읽었습니까? 먼저 StartTCP();를 호출하기 전에 FirstWindow.Dispatcher에 대한 참조를 얻습니다. 그 호출 후 Dispatcher.BeginInvoke (System.Windows.Threading.DispatcherPriority.SystemIdle,() => FirstWindow.Close()); –

관련 문제