ReadAsync - 线程退出,代码为259

时间:2014-04-01 03:24:04

标签: c# multithreading sockets windows-phone-8 bluetooth

我遇到蓝牙连接问题。它是无限连接,几秒钟后它总是挂起ReadAsync信息:

  

线程0x19c已退出,代码为259(0x103)。   该程序' [5612] TaskHost.exe'已退出代码0(0x0)。

此后 - 连接对设备仍然有效(我在设备上看到连接二极管),但没有机会: 1)再次连接到设备(连接仍然存在) 2)从套接字/连接

接收任何数据

总是挂在这里:

   returnedBuffer =
                await
                    socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length,
                        InputStreamOptions.Partial);

Modyfied代码来自32英尺样本:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var bdp = new BluetoothDevicePicker();
        PeerInformation pi = await bdp.PickDeviceAsync();
        if (pi != null)
        {
            var hn = new HostName(pi.HostName.RawName);

            // do something with the device
            ReadThread(hn);
        }
    }

    private async void ReadThread(object host)
{
    reading = true;

    var socket = new StreamSocket();
    socket.Control.KeepAlive = true;
    socket.Control.NoDelay = false;
    //socket.Control.QualityOfService = SocketQualityOfService.LowLatency;

    IAsyncAction a = StreamSocketExtensions.ConnectAsync(socket, (HostName)host, RfcommServiceId.SerialPort);
    await a;

    var buffer = new byte[2048];

    while (reading)
    {
        IBuffer returnedBuffer = null;
        try
        {
            returnedBuffer =
                await
                    socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)buffer.Length,
                        InputStreamOptions.Partial);
            if (returnedBuffer != null)
            {

                string s = PrintByteArray(returnedBuffer.ToArray(), (int)returnedBuffer.Length);

                if (!string.IsNullOrEmpty(s))
                {


                    Dispatcher.BeginInvoke(new Action<string>(InsertMessage), s);


                }
            }
        }
        catch
        {

        }

    }

    socket.Dispose();
}

0 个答案:

没有答案