我正在尝试使用 Visual Studio 2012 创建一个 Windows 应用程序,但似乎正在发生奇怪的事情......当我在控制台应用程序中运行完全相同的代码时,它工作正常,但似乎我无法输出以下内容一次我在 Windows 应用程序项目的线程中运行它:
private void VisualUDPListener_Load(object sender, EventArgs e)
{
//System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
new Thread(delegate()
{
StartListener();
}).Start();
}
private void StartListener()
{
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (true)
{
//log text box
Log.AppendText("Listening \n");
byte[] bytes = listener.Receive(ref groupEP);
string hex_string = BitConverter.ToString(bytes);//this works and returns the correct hex data
string ascii_string = Encoding.ASCII.GetString(bytes, 0, bytes.Length);//blank???????????
MessageBox.Show(ascii_string.Length.toString());//outputs 131 which is correct
MessageBox.Show(ascii_string);// shows a blank message box
Log.AppendText("--" + ascii_string + hex_string +" \n");//only outputs --
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
我的目标是 .NET Framework 4.5 ...多于。(那么设备必须发送损坏的数据?不,因为如果上面的代码在控制台应用程序中运行,它会完美运行并输出正确的字符串)
任何帮助将不胜感激。