以下是有问题的方法:
private async void _reader_MultiSourceFrameArrived(object sender,MultiSourceFrameArrivedEventArgs e) { MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame(); using (var colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame()) { if (colorFrame != null) { _writeableBitmap.Lock(); colorFrame.copyConvertedFrameDataToIntPtr( _writeableBitmap.BackBuffer,(uint)(_colorFrameDescription.Width * _colorFrameDescription.Height * _colorFrameDescription.BytesPerPixel),ColorImageFormat.Bgra); _writeableBitmap.AddDirtyRect(new Int32Rect(0,_writeableBitmap.PixelWidth,_writeableBitmap.PixelHeight)); _writeableBitmap.Unlock(); reflectionOverlayControl.ReflectionImageSource = _writeableBitmap; } } using (var bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame()) { if (bodyFrame != null) { Body body = JointHelpers.FindClosestBody(bodyFrame); if (body != null) { if (body.IsTracked) { Dictionary<BodyComponentType,BodyComponent> bodyComponentDictionary = BuildBodyComponentDictionary(body); foreach (BodyComponent bodyComponent in bodyComponentDictionary.Values.OrderBy(x => x.BodyComponentType)) { bodyComponent.Generate(_writeableBitmap,_coordinateMapper,FrameType.Color,25); if (!_isRunningFiltering) { _isRunningFiltering = true; try { await Task.Run(() => { bodyComponent.RunFunFiltering(); }); } finally { _isRunningFiltering = false; } } } reflectionOverlayControl.UpdateValues( bodyComponentDictionary,GetFullBodyComponent(body)); } } } } }
现在,请允许我解释一下:
>当一个特定类型的帧从Kinect到达时,该方法运行,这是获取的,我可以在使用块中从中提取ColorFrame和BodyFrame.
>第一个“using”块将ColorFrame转换为WPF WriteableBitmap(在构造函数中声明),并将用户控件的ReflectionImageSource设置为等于此WriteableBitmap.如果这是唯一的使用块,我会在屏幕上看到非常平滑的馈送!
>第二个BodyFrame使用确定最接近的主体,如果它被跟踪,然后创建一个填充有人BodyComponents(手,脚,头等)的字典
>这里的foreach循环在每个BodyComponent上运行“Generate”函数,该函数设置了它的一些属性.例如,它设置一个EncompassingRectangle属性,该属性是一个旨在包含该组件的Int32Rect对象.
接下来是我需要帮助的地方!
RunFunFiltering方法是一种密集型处理方法,在运行时会创建一个冻结我的UI的阻塞语句.这会使我的彩色帧视频输入非常跳跃!此RunFunFiltering方法需要设置一些BodyComponent类的属性,例如矩形应显示的颜色,其中ReflectionImageSource中的白色像素数,以及使用第一个ReflectionImageSource的一部分设置另一个可写位图.长方形.
由于此对象现在已完成,并且已设置所有属性(并且已对字典中的每个BodyComponent执行此操作),因此我在视图上运行UpdateValues方法,该方法在屏幕上显示BodyComponent类中的有趣内容.
根据@sstan在这篇文章中的一些建议:Async Await to Keep Event Firing
我扔了一个Task.Run()块.但是,这似乎没有释放我的UI,我仍然看到一个跳跃的图像.奇怪的是在那个计时器例子中,它完美地工作!我在这里有点不知所措.
我是一个有异步功能的初学者,但我真的很想了解你的解决方案.如果您能对代码提供解释,我将非常感激!
更新
我已经能够识别出获取帧的using语句在将UI放置在Task.Run调用之外时阻止了UI.
我不能只使用块异步运行整个BodyFrame因为我需要第一个“生成”函数始终发生而不是重处理线程的一部分.两个使用积木似乎不优雅,而是在地毯下推动我的问题……
解决方法
>您有一个帧到达时调用的异步函数
>如果没有运行RunFunFiltering任务,则启动一个
>如果此任务正在运行,请不要启动新任务
>如果RunFunFiltering结束处理结果
.
Task taskFunFiltering = null; private async Task ProcessFrame(...) { // a new frame is arrived DoSomeProcessing(...); // only start a new run fun filtering if prevIoUs one is finished if (taskFunFiltering == null || taskFunFiltering.IsCompleted) { // start a new fun filtering // don't wait for the result taskFunFiltering = Task.Run( () => ...); } } private async Task RunFunFiltering(...) { // do the filtering and wait until finished var filterResult = await DoFiltering(...); displayResult(filterResult); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。