提交线程休眠,异常捕获代码

This commit is contained in:
gil zhang
2022-11-20 23:04:28 +08:00
parent 0b1128d065
commit 7828c5790c
5 changed files with 65 additions and 39 deletions
+2 -1
View File
@@ -195,11 +195,12 @@ public partial class App : Application
IntelligenceService.Current.CleanUp();
await CameraService.Current.CleanupMediaCaptureAsync();
EmojiPlayHelper.Current.CancellationToken.Cancel();
}
catch (Exception)
{
}
System.Environment.Exit(0);
}
}
@@ -1,18 +1,20 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Verdure.ElectronBot.Core.Models;
using Verdure.ElectronBot.Core.Models;
namespace ElectronBot.BraincasePreview.Helpers;
public class EmojiPlayHelper
{
private static EmojiPlayHelper _current;
private static EmojiPlayHelper? _current;
public static EmojiPlayHelper Current => _current ??= new EmojiPlayHelper();
private readonly Queue<EmoticonActionFrame> _actonFrame = new();
private readonly object _actonFrameLock = new();
public CancellationTokenSource CancellationToken
{
get; set;
} = new();
public int Interval
{
get; set;
@@ -20,10 +22,14 @@ public class EmojiPlayHelper
public void Start()
{
var thread = new Thread(RunPlayAsync);
// var thread = new Thread(RunPlayAsync, CancellationToken.Token);
var task = new Task(RunPlayAsync, CancellationToken.Token);
task.Start();
//thread.IsBackground = true;
thread.Start();
//thread.Start();
}
public void Clear()
@@ -63,6 +69,10 @@ public class EmojiPlayHelper
Thread.Sleep(Interval);
}
else
{
Thread.Sleep(3000);
}
}
}
@@ -1,19 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using ElectronBot.BraincasePreview.Models;
using Microsoft.AI.MachineLearning;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Media.FaceAnalysis;
using Windows.Storage;
using Windows.UI.Core;
namespace ElectronBot.BraincasePreview.Services;
public class IntelligenceService
@@ -107,7 +100,8 @@ public class IntelligenceService
{
if (!_isInitialized)
{
throw new InvalidOperationException("Service not initialized.");
return;
//throw new InvalidOperationException("Service not initialized.");
}
CameraService.Current.SoftwareBitmapFrameCaptured -= Current_SoftwareBitmapFrameCaptured;
@@ -183,6 +183,12 @@ public partial class MainViewModel : ObservableRecipient, INavigationAware
}
Thread.Sleep(1000);
if (ElectronBotHelper.Instance.SerialPort.IsOpen)
{
ElectronBotHelper.Instance.SerialPort.Close();
}
}
[RelayCommand]
+38 -23
View File
@@ -285,26 +285,34 @@ public class ElectronLowLevel : IElectronLowLevel
var _packetCount = packetCount;
do
try
{
ErrorCode ec;
do
{
var readBuffer = new byte[packetSize];
ErrorCode ec;
do
{
var readBuffer = new byte[packetSize];
ec = reader!.Read(readBuffer, 5000, out _);
ec = reader!.Read(readBuffer, 5000, out _);
extraDataBufferRx = readBuffer;
extraDataBufferRx = readBuffer;
} while (ec != ErrorCode.Success);
} while (ec != ErrorCode.Success);
_packetCount--;
_packetCount--;
} while (_packetCount > 0);
} while (_packetCount > 0);
stopwatch.Stop();
stopwatch.Stop();
Console.WriteLine($"time- ReceivePacket time{stopwatch.ElapsedMilliseconds}");
Console.WriteLine($"time- ReceivePacket time{stopwatch.ElapsedMilliseconds}");
}
catch (Exception)
{
// todo:异常处理
}
return _packetCount == 0;
}
@@ -318,29 +326,36 @@ public class ElectronLowLevel : IElectronLowLevel
var dataOffset = 0;
do
try
{
ErrorCode ec;
do
{
ec = writer!.Write(buffer, frameBufferOffset + dataOffset, packetSize, 2000, out _);
if (packetSize % 512 == 0)
ErrorCode ec;
do
{
ec = writer.Write(buffer, frameBufferOffset + dataOffset, 0, 2000, out _);
}
ec = writer!.Write(buffer, frameBufferOffset + dataOffset, packetSize, 2000, out _);
} while (ec != ErrorCode.None);
if (packetSize % 512 == 0)
{
ec = writer.Write(buffer, frameBufferOffset + dataOffset, 0, 2000, out _);
}
dataOffset += packetSize;
} while (ec != ErrorCode.None);
_packetCount--;
dataOffset += packetSize;
} while (_packetCount > 0);
_packetCount--;
stopwatch.Stop();
} while (_packetCount > 0);
Console.WriteLine($"time- TransmitPacket time{stopwatch.ElapsedMilliseconds}");
stopwatch.Stop();
Console.WriteLine($"time- TransmitPacket time{stopwatch.ElapsedMilliseconds}");
}
catch (Exception)
{
// todo:异常处理
}
return _packetCount == 0;
}