Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 异步任务赢得';while循环==false之后的t finish方法_C#_Xamarin - Fatal编程技术网

C# 异步任务赢得';while循环==false之后的t finish方法

C# 异步任务赢得';while循环==false之后的t finish方法,c#,xamarin,C#,Xamarin,也许是个奇怪的问题,但我似乎想不出来。我从bluetoothstream接收数据,在while循环中读取所有传入数据后,我想比较此消息。但结果是它无法完成其余的代码。代码如下所示: public void beginListenForData() { try { inStream = btSocket.InputStream; } catch (IOException ex) {

也许是个奇怪的问题,但我似乎想不出来。我从bluetoothstream接收数据,在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循环中,并完成其余的代码。但这似乎不会发生,它不会完成返回调用方法的代码,不会出现任何错误


提前感谢是因为您重复嵌套while(true)并调用了
bytes=wait inStream.ReadAsync(buffer,0,buffer.Length)当您循环
inStream.ReadAsync

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

 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);

是因为您重复嵌套while(true)并调用
bytes=wait inStream.ReadAsync(buffer,0,buffer.Length)当您循环
inStream.ReadAsync

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

 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);

因此,对于任何一个在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;
    }
public void beginListenForData()
{
尝试
{
inStream=btSocket.InputStream;
}
捕获(IOEX异常)
{
控制台写入线(例如消息);
}
Task.Run(异步()=>
{
while(true)
{
尝试
{
if(inStream.IsDataAvailable()==true)
{
bytes=wait inStream.ReadAsync(buffer,0,buffer.Length);
eindtekst=String.Concat(eindtekst,Encoding.ASCII.GetString(缓冲区,0,字节));
Array.Clear(buffer,0,buffer.Length);
如果(eindtekst==“收到”)
{
Xamarin.Forms.MessagingCenter.Send(此为“已接收”,true);
}
否则,如果(eindtekst==“D0O”)
{
Xamarin.Forms.MessagingCenter.Send((IBluetoothService)这个“redButtonClicked”);
}
否则如果(eindtekst==“StopTijd”)
{
Xamarin.Forms.MessagingCenter.Send((IBluetoothService)这个“Win”);
}
eindtekst=“”;
}
}
捕获(例外e)
{
系统.诊断.调试.写入线(“Fout-bij-het-ontvangen”+e.Message);
打破
}
}
}).配置等待(错误);
返回;
}

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

因此,对于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;
    }
public void beginListenForData()
{
尝试
{
inStream=btSocket.InputStream;
}
捕获(IOEX异常)
{
控制台写入线(例如消息);
}
Task.Run(异步()=>
{
while(true)
{
尝试
{
if(inStream.IsDataAvailable()==true)
{
bytes=wait inStream.ReadAsync(buffer,0,buffer.Length);
eindtekst=String.Concat(eindtekst,Encoding.ASCII.GetString(缓冲区,0,字节));
Array.Clear(buffer,0,buffer.Length);
如果(eindtekst==“收到”)
{
Xamarin.Forms.MessagingCenter.Send(此为“已接收”,true);
}
否则,如果(eindtekst==“D0O”)
{
Xamarin.Forms.MessagingCenter.Send((IBluetoothService)这个“redButtonClicked”);
}
否则如果(eindtekst==“StopTijd”)
{
Xamarin.Forms.MessagingCenter.Send((IBluetoothService)这个“Win”);
}
eindtekst=“”;
}
}
捕获(例外e)
{
系统.诊断.调试.写入线(“Fout-bij-het-ontvangen”+e.Message);
打破
}
}
}).配置等待(错误);
返回;
}

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

这条溪流真的被关闭了吗?“当前没有更多数据可用”与“没有更多数据可用且输入流标记为关闭”之间存在巨大差异-只有后者返回0;前者将永远等待(除非指定了超时)-或者更确切地说:它将等待,直到至少有一个字节可用,或者它变为关闭。流尚未关闭,但我的想法是,如果流中没有数据,则应该关闭。但当bytes==0时,它应该关闭。但是当我尝试使用if(bytes==0)而不是close时,它永远不会进入if状态