Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# c Android-ISurfaceTextureListener线程中断,然后在活动关闭时恢复_C#_Android_Multithreading_Asynchronous_Interruption - Fatal编程技术网

C# c Android-ISurfaceTextureListener线程中断,然后在活动关闭时恢复

C# c Android-ISurfaceTextureListener线程中断,然后在活动关闭时恢复,c#,android,multithreading,asynchronous,interruption,C#,Android,Multithreading,Asynchronous,Interruption,我有一个带有ISurfaceTextureListener的活动,该活动具有一个异步OnSurfaceTextureUpdated方法,在该方法中我调用异步SendPixel方法: public async void OnSurfaceTextureUpdated(SurfaceTexture surface) { bool sendPixel = false; [...] if (Settings.Pixel.period >= 0) {

我有一个带有ISurfaceTextureListener的活动,该活动具有一个异步OnSurfaceTextureUpdated方法,在该方法中我调用异步SendPixel方法:

public async void OnSurfaceTextureUpdated(SurfaceTexture surface)
{
    bool sendPixel = false;

    [...]

    if (Settings.Pixel.period >= 0)
    {
        if (stopWatch.ElapsedMilliseconds >= Settings.Pixel.nextSending)
        {
            if (Settings.Pixel.period > 0)
                Settings.Pixel.nextSending += Settings.Pixel.period * ((int)((stopWatch.ElapsedMilliseconds - Settings.Pixel.nextSending) / Settings.Pixel.period) + 1);
            sendPixel = true;
        }
    }

    [...]

    if (sendPixel)
        await SendPixel();
}
async Task SendPixel()
{
    try
    {
        if (Ev3Messaging.IsConnected())
        {
            Color color = new Color(bm.GetPixel(Settings.Camera.viewport.X_VpToCv(Settings.Pixel.x), Settings.Camera.viewport.X_VpToCv(Settings.Pixel.y)));
            Ev3Messaging.AddNumberMessage("MC_Hue", color.GetHue());
            Ev3Messaging.AddNumberMessage("MC_Brightness", color.GetBrightness());
            Console.WriteLine("SendPixel");
            await Ev3Messaging.SendMessages();
            Console.WriteLine("SendPixelEnd");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error SendPixel: " + ex.Message);
    }
}
以下是异步SendPixel方法:

public async void OnSurfaceTextureUpdated(SurfaceTexture surface)
{
    bool sendPixel = false;

    [...]

    if (Settings.Pixel.period >= 0)
    {
        if (stopWatch.ElapsedMilliseconds >= Settings.Pixel.nextSending)
        {
            if (Settings.Pixel.period > 0)
                Settings.Pixel.nextSending += Settings.Pixel.period * ((int)((stopWatch.ElapsedMilliseconds - Settings.Pixel.nextSending) / Settings.Pixel.period) + 1);
            sendPixel = true;
        }
    }

    [...]

    if (sendPixel)
        await SendPixel();
}
async Task SendPixel()
{
    try
    {
        if (Ev3Messaging.IsConnected())
        {
            Color color = new Color(bm.GetPixel(Settings.Camera.viewport.X_VpToCv(Settings.Pixel.x), Settings.Camera.viewport.X_VpToCv(Settings.Pixel.y)));
            Ev3Messaging.AddNumberMessage("MC_Hue", color.GetHue());
            Ev3Messaging.AddNumberMessage("MC_Brightness", color.GetBrightness());
            Console.WriteLine("SendPixel");
            await Ev3Messaging.SendMessages();
            Console.WriteLine("SendPixelEnd");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error SendPixel: " + ex.Message);
    }
}
ISurfaceTextureListener读取视频流,然后多次调用SendPixel方法。 问题是控制台很好地显示SendPixel,但不显示SendPixelEnd,并且不会引发错误。 之后,当我关闭“活动”时,将显示每个缺少的SendPixelEnd

编辑:EV3Messaging.SendMessages通过前几行中添加的蓝牙发送消息:

static List<byte> btMessage = new List<byte>();
static public async Task SendMessages()
{
    if (_socket != null)
        await _socket.OutputStream.WriteAsync(btMessage.ToArray(), 0, btMessage.Count);
}
为什么会发生这种情况?我该如何解决

提前谢谢


PS:抱歉我的英语不好^ ^

问题解决:每次发送数据时,我都忘记清除btMessage列表,这导致了这种奇怪的行为。此问题也可能是因为从蓝牙设备接收数据的方式不正确

然后SendMessages方法变为:

static public async Task SendMessages()
{
    if (_socket != null)
        await _socket.OutputStream.WriteAsync(btMessage.ToArray(), 0, btMessage.Count);
    btMessage.Clear();
}
因为每次我添加消息时,新帧都会添加到消息列表的末尾

此外,为了获取传入消息,我使用以下代码:

public static void InputMessagesManager(Action<BtMessage> incomingMessageEvent)
{
    _tokenSource = new CancellationTokenSource();
    Task t = Task.Factory.StartNew(async () =>
    {
        Stream inputStream = _socket.InputStream;

        while (!_tokenSource.IsCancellationRequested)
        {
            if (inputStream == null)
                inputStream = _socket.InputStream;
            if (inputStream != null && inputStream.CanRead)
            {
                await inputStream.ReadAsync(_sizeBuffer, 0, _sizeBuffer.Length);

                short size = (short)(_sizeBuffer[0] | _sizeBuffer[1] << 8);
                if (size != 0)
                {
                    byte[] frame = new byte[size];
                    await inputStream.ReadAsync(frame, 0, frame.Length);
                    BtMessage btMes = new BtMessage(frame);
                    incomingMessageEvent(btMes);
                    Console.WriteLine("Received: " + btMes.name + " -> " + btMes.getValueAsNumber());
                }
            }
        }
    }, _tokenSource2.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}

如果没有Ev3Messaging.SendMessages上的代码,则无法确定原因。哦,抱歉,我将添加它。看来蓝牙流不起作用,当您关闭应用程序时,由于套接字已关闭,所有操作都将结束。您是否在连接的设备上接收到消息?只接收到其中一些消息,我刚刚意识到一些SendPixelEnd显示在SendPixels之间,当然与接收到的消息相关。。。。当活动关闭而不是应用程序关闭时,蓝牙设备会接收所有发送的消息。尽量不要使用InputStream和OutputStream属性,只检索一次,并在任何地方使用检索到的实例,你不知道这些属性在引擎盖下做什么,每次调用它们时,它们可能会构造一个新的流。另外,在连接关闭之前,不要使用计时器读取、创建线程或任务并持续读取。从输入流读取不应干扰输出流。