fix: 尝试修复示波器无法关闭的问题
This commit is contained in:
parent
f23a8a9712
commit
62c16c016d
|
@ -69,9 +69,8 @@ class OscilloscopeScanTaskInfo
|
|||
{
|
||||
public Task? ScanTask { get; set; }
|
||||
public OscilloscopeCtrl Client { get; set; }
|
||||
public CancellationTokenSource CTS { get; set; } = new();
|
||||
public CancellationTokenSource CTS { get; set; } = new CancellationTokenSource();
|
||||
public int Frequency { get; set; } = 100;
|
||||
public bool IsRunning { get; set; } = false;
|
||||
|
||||
public OscilloscopeScanTaskInfo(OscilloscopeCtrl client)
|
||||
{
|
||||
|
@ -148,8 +147,9 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
}
|
||||
}
|
||||
|
||||
private Task ScanTask(OscilloscopeScanTaskInfo taskInfo, CancellationToken token)
|
||||
private Task ScanTask(OscilloscopeScanTaskInfo taskInfo, string clientId)
|
||||
{
|
||||
var token = taskInfo.CTS.Token;
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
|
@ -161,10 +161,14 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
continue;
|
||||
}
|
||||
|
||||
await _hubContext.Clients.Client(Context.ConnectionId).OnDataReceived(data);
|
||||
await _hubContext.Clients.Client(clientId).OnDataReceived(data);
|
||||
await Task.Delay(1000 / taskInfo.Frequency, token);
|
||||
}
|
||||
}, token);
|
||||
}, token).ContinueWith(t =>
|
||||
{
|
||||
if (t.IsFaulted)
|
||||
logger.Error(t.Exception, "ScanTask failed");
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<bool> StartCapture()
|
||||
|
@ -175,7 +179,7 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
var key = (board.ID.ToString(), Context.ConnectionId);
|
||||
var client = GetOscilloscope().OrThrow(() => new Exception("Oscilloscope not found"));
|
||||
|
||||
if (_scanTasks.TryGetValue(key, out var existing) && existing.IsRunning)
|
||||
if (_scanTasks.TryGetValue(key, out var existing))
|
||||
return true;
|
||||
|
||||
var result = await client.SetCaptureEnable(true);
|
||||
|
@ -186,8 +190,7 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
}
|
||||
|
||||
var scanTaskInfo = new OscilloscopeScanTaskInfo(client);
|
||||
var token = scanTaskInfo.CTS.Token;
|
||||
scanTaskInfo.ScanTask = ScanTask(scanTaskInfo, token);
|
||||
scanTaskInfo.ScanTask = ScanTask(scanTaskInfo, Context.ConnectionId);
|
||||
|
||||
_scanTasks[key] = scanTaskInfo;
|
||||
|
||||
|
@ -211,7 +214,6 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
|
||||
if (_scanTasks.TryRemove(key, out var scanInfo))
|
||||
{
|
||||
scanInfo.IsRunning = false;
|
||||
scanInfo.CTS.Cancel();
|
||||
if (scanInfo.ScanTask != null) await scanInfo.ScanTask;
|
||||
scanInfo.CTS.Dispose();
|
||||
|
@ -384,7 +386,7 @@ public class OscilloscopeHub : Hub<IOscilloscopeReceiver>, IOscilloscopeHub
|
|||
var board = TryGetBoard().OrThrow(() => new Exception("Board not found"));
|
||||
var key = (board.ID.ToString(), Context.ConnectionId);
|
||||
|
||||
if (_scanTasks.TryGetValue(key, out var scanInfo) && scanInfo.IsRunning)
|
||||
if (_scanTasks.TryGetValue(key, out var scanInfo))
|
||||
{
|
||||
scanInfo.Frequency = frequency;
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue