我遇到用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;
}
我做错了什么?
提前谢谢!