2

我在使用 C# 编写的 Win8 Metro 应用程序时遇到问题。我有 UDP 侦听器,它从 WP7 设备接收数据包(简单的一个字符串命令)。

我可以看到数据包正在到达应用程序,但我只能收到其中几个。在输出窗口中经过一段时间(5-10 秒)后,我收到

线程 0x5a4 以代码 0 (0x0) 退出。

我不明白为什么线程停止。这是我的源代码,很简单。我绑定了侦听器,设置事件处理程序,然后当我收到命令时,我只是在 UI 上移动椭圆形状(用户)。

代码 :

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
    DatagramSocket listener = new DatagramSocket();          
    listener.MessageReceived += listener_MessageReceived;            

    try
    {
        await listener.BindServiceNameAsync(serviceName.Text);            
    }
    catch (Exception exception)          
    {               
        if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
        {
            throw;
        }                
    }                
}

async void listener_MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
{
    try
    {
        uint stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
        string command = eventArguments.GetDataReader().ReadString(stringLength);

        await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                MessageBlock.Text = "Message : " + command + " (@ " + DateTime.Now;
                MakeMove(command); 
            }
        );            
    }
    catch (Exception exception)
    {
        SocketErrorStatus socketError = SocketError.GetStatus(exception.HResult);
        if (socketError == SocketErrorStatus.ConnectionResetByPeer)
        {                     
            Debug.WriteLine("Peer does not listen on the specific port. Please make sure that you run step 1 first " +
                        "or you have a server properly working on a remote server."); 
        }
        else if (socketError != SocketErrorStatus.Unknown)
        {
            Debug.WriteLine("Error happened when receiving a datagram: " + socketError.ToString());
        }
        else
        {
            throw;
        }
    }
}

private void MakeMove(string command)
{
    Debug.WriteLine(command);

    switch (command)
    {
        case "10":
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) - 50);
            break;
        case "20":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) + 50);
            break;
        case "30":
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) + 50);
            break;
        case "40":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) - 50);
            break;
        case "50":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) + 50);
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) - 50);
            break;
        case "60":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) - 50);
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) - 50);
            break;
        case "70":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) + 50);
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) + 50);
            break;
        case "80":
            this.user.SetValue(Canvas.LeftProperty, Convert.ToInt32(this.user.GetValue(Canvas.LeftProperty)) - 50);
            this.user.SetValue(Canvas.TopProperty, Convert.ToInt32(this.user.GetValue(Canvas.TopProperty)) + 50);
            break;
        default:
            Debug.WriteLine("Unknown command");
            break;
    }

我做错了什么?

先感谢您!

4

0 回答 0