【问题标题】:Async task won't finish method after while loop == false在 while 循环 == false 之后,异步任务不会完成方法
【发布时间】:2020-03-30 10:12:21
【问题描述】:

也许是一个奇怪的问题,但我似乎无法弄清楚。我正在从蓝牙流接收数据,在我读取了 while 循环内的所有传入数据后,我想比较此消息。但是发生的事情是它不会完成其余的代码。代码如下:

 public void beginListenForData()
    {
        try
        {
            inStream = btSocket.InputStream;
        }
        catch (IOException ex)
        {
            Console.WriteLine(ex.Message);
        }
        Task.Run(async() =>
        {
            while (true)
            {
                try
                {
                    bytes = await inStream.ReadAsync(buffer, 0, buffer.Length);

                    while (bytes != 0)
                    {
                        eindtekst = String.Concat(eindtekst, Encoding.ASCII.GetString(buffer, 0, bytes));
                        Array.Clear(buffer, 0, buffer.Length);
                        bytes = await inStream.ReadAsync(buffer, 0, buffer.Length);
                    }

                    if (eindtekst == "D0O")
                    {
                        Array.Clear(buffer, 0, buffer.Length);
                        Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "redButtonClicked");
                        eindtekst = "";
                        inStream.Flush();
                        inStream.Dispose();
                        inStream.Close();
                    }
                    else if (eindtekst == "StopTijd")
                    {
                        Array.Clear(buffer, 0, buffer.Length);
                        Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "Win");
                        eindtekst = "";
                        inStream.Flush();
                        inStream.Dispose();
                        inStream.Close();
                    }

                    if (bytes == 0)
                    {
                        eindtekst = "";
                        inStream.Flush();
                        inStream.Dispose();
                        inStream.Close();
                    }
                }
                catch (IOException e)
                {
                System.Diagnostics.Debug.WriteLine("Fout bij het ontvangen " + e.Message);
                break;
                }
            }
    }).ConfigureAwait(false);
        return;
    }

我期望的是,在 while == 0 之后,它会在这个 while 循环内停止并完成其余的代码。但这似乎没有发生,它不会完成返回调用方法的代码而没有任何错误左右。

提前致谢

【问题讨论】:

  • 流真的被关闭了吗? “当前没有更多数据可用”与“没有更多数据可用并且输入流被标记为关闭”之间存在巨大差异 - 只有后者返回 0;前者将永远等待(除非指定了超时) - 或者更确切地说:它将等待直到 要么 至少有一个字节可用,或者它变为 关闭
  • 流还没有关闭,但我的想法是,如果流中没有数据,那么应该关闭。但是当字节 == 0 时它应该被关闭。但是当我尝试使用 if(bytes == 0) 而不是关闭它时,它永远不会进入 if 语句
  • 1. while (bytes != 0) 循环结束了吗? 2. 尝试将catch (Exception e) 添加到try 块?可能是IOException 以外的异常被抛出,因此while (bytes != 0) 循环之后的代码不会被执行。
  • 也许我错了。我不认为while (bytes != 0) 是正确的。终止循环的条件应该是bytes < buffer.Length?
  • 1.不,它没有结束 2. 我会尝试更改异常类型,如果我知道更多,会更新。 Buffer.length = 1024,我使用的字符串最大为 8,这应该不是问题。我使用 while (bytes!=0) 的原因是 bcs 当 instream.readasync() == 0 没有可用数据时,循环通过 instream amd place IT info buffer while instream != 0 (no data)

标签: c# xamarin


【解决方案1】:

是不是因为你在循环inStream.ReadAsync时反复嵌套了while(true)并调用了bytes = await inStream.ReadAsync(buffer, 0, buffer.Length);

我没有测试它是否有效。你的循环方法看起来不是很清楚,也许你可以试试下面:

 Task.Run(async() =>
    {        
         int bytes = 0;
           try
            {

                while (bytes =await inStream.ReadAsync(buffer, 0, buffer.Length) > 0)
                {
                    eindtekst = String.Concat(eindtekst, Encoding.ASCII.GetString(buffer, 0, bytes));
                    Array.Clear(buffer, 0, buffer.Length);

                }

                if (eindtekst == "D0O")
                {
                    Array.Clear(buffer, 0, buffer.Length);
                    Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "redButtonClicked");
                    eindtekst = "";
                    inStream.Flush();
                    inStream.Dispose();
                    inStream.Close();
                }
                else if (eindtekst == "StopTijd")
                {
                    Array.Clear(buffer, 0, buffer.Length);
                    Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "Win");
                    eindtekst = "";
                    inStream.Flush();
                    inStream.Dispose();
                    inStream.Close();
                }

                if (bytes == 0)
                {
                    eindtekst = "";
                    inStream.Flush();
                    inStream.Dispose();
                    inStream.Close();
                }
            }
            catch (IOException e)
            {
            System.Diagnostics.Debug.WriteLine("Fout bij het ontvangen " + e.Message);
            break;
            }
}).ConfigureAwait(false);

【讨论】:

  • 感谢您的回答,但很遗憾,这似乎不起作用。但我会发布我的答案如何解决它。感谢您的努力
【解决方案2】:

因此,对于任何在 xamarin 中遇到蓝牙问题的人,我刚刚找到了答案。这只是从其他设备(如 Arduino、pic 微控制器、Raspberry pi 等)读取数据的“简单”方法。我认为关于写入数据的信息已经足够多,所以这应该不是问题。但是我的回答是从其他设备接收数据。

 public void beginListenForData()
    {
        try
        {
            inStream = btSocket.InputStream;
        }
        catch (IOException ex)
        {
            Console.WriteLine(ex.Message);
        }
        Task.Run(async () =>
        {
            while (true)
            {
                try
                {
                    if (inStream.IsDataAvailable() == true)
                    {
                        bytes = await inStream.ReadAsync(buffer, 0, buffer.Length);
                        eindtekst = String.Concat(eindtekst, Encoding.ASCII.GetString(buffer, 0, bytes));
                        Array.Clear(buffer, 0, buffer.Length);

                        if (eindtekst == "Received")
                        {
                            Xamarin.Forms.MessagingCenter.Send<IBluetoothService, bool>(this, "Received", true);
                        }
                        else if (eindtekst == "D0O")
                        {
                            Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "redButtonClicked");
                        }
                        else if (eindtekst == "StopTijd")
                        {
                            Xamarin.Forms.MessagingCenter.Send((IBluetoothService)this, "Win");
                        }
                        eindtekst = "";
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Fout bij het ontvangen " + e.Message);
                    break;
                }
            }
        }).ConfigureAwait(false);
        return;
    }

我希望这会从长远来看对某些人有所帮助。

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2019-02-14
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多