C
C#2y ago
Halfbax

❔ Data doesnt get set with DispatcherQueue.TryEnqueue()

I try to get my camera feed with opencv on a non UI thread in WinUI 3. It's weird the data gets pushed into the queue, but the function doesn't get fired. I start CaptureAsync with Task.Run() because when I start it with CaptureAsync().ConfigureAwait(false); the UI gets blocked even with Task.Yield.
private async Task CaptureAsync()
{
_cts.TryReset();

if (CurrentSource is null)
return;

DispatcherQueue? queue = DispatcherQueue.GetForCurrentThread();
try
{
_capture = VideoCapture.FromCamera(CurrentSource.Id);

byte[] data;
while (!_cts.IsCancellationRequested)
{
using Mat? image = _capture.RetrieveMat();

if (image.Height <= 0)
continue;

data = image.ToBytes();
queue?.TryEnqueue(() => SetData(data)); // data != null, checked

await Task.Yield();
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

// doesnt get fired once
private void SetData(byte[] data)
=> CurrentFrame = data;
private async Task CaptureAsync()
{
_cts.TryReset();

if (CurrentSource is null)
return;

DispatcherQueue? queue = DispatcherQueue.GetForCurrentThread();
try
{
_capture = VideoCapture.FromCamera(CurrentSource.Id);

byte[] data;
while (!_cts.IsCancellationRequested)
{
using Mat? image = _capture.RetrieveMat();

if (image.Height <= 0)
continue;

data = image.ToBytes();
queue?.TryEnqueue(() => SetData(data)); // data != null, checked

await Task.Yield();
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

// doesnt get fired once
private void SetData(byte[] data)
=> CurrentFrame = data;
I do have two options. Fix the Task.Yield() problem or let it run on a secondary thread and fix the queue data. What would you do?
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server