这是代码的外观。
void ApplyFilter()
{
this.filterOperationInProgress = true;
WriteableBitmap wBmp = new WriteableBitmap(0, 0);
wBmp = LocalWriteableBitmap.Clone();
NokiaFilters nf = new NokiaFilters(wBmp, FilterName.Mango);
wBmp = nf.render().Result;
this.filterOperationInProgress = false;
}
诺基亚过滤器类:
class NokiaFilters
{
WriteableBitmap wb;
FilterName filtername = FilterName.NoFilter;
public NokiaFilters(WriteableBitmap wbm1, FilterName filtername1)
{
wb = wbm1;
filtername = filtername1;
}
public async Task<WriteableBitmap> render()
{
MemoryStream memstream = new MemoryStream(wb.ToByteArray());
if (memstream == null)
return wb;
//Mango
else if (filtername == FilterName.Mango)
{
memstream.Position = 0;
// Create a source to read the image from PhotoResult stream
using (var source = new StreamImageSource(memstream))
{
// Create effect collection with the source stream
using (var filters = new FilterEffect(source))
{
// Initialize the filter
ContrastFilter contrastfilter = new ContrastFilter(0.16);
HueSaturationFilter saturationfilter = new HueSaturationFilter(-0.01, -0.53);
LomoFilter lomofilter = new LomoFilter(0.5, 0.5, LomoVignetting.Low, LomoStyle.Yellow);
// Add the filter to the FilterEffect collection
filters.Filters = new List<IFilter> { contrastfilter, saturationfilter, lomofilter };
// Create a target where the filtered image will be rendered to
var target = new WriteableBitmap((int)wb.PixelWidth, (int)wb.PixelHeight);
// Create a new renderer which outputs WriteableBitmaps
using (var renderer = new WriteableBitmapRenderer( filters,target))
{
// Render the image with the filter(s)
try
{
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
}
catch (System.InvalidOperationException inv)
{ }
catch { }
// Set the output image to Image control as a source
return target;
}
}
}
}
return wb;
}
}
我的 UI 完全卡住了,甚至后退按钮也没有响应。当我停止调试器时,代码指向
wBmp = nf.render().Result;
在这种情况发生之前开始执行的语句是
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
我是并行编程的新手。我搜索了很多,发现死锁在这种情况下很常见。我相信这也是一个死锁的情况。我该如何解决这个问题?
更新:
更换后
wBmp = nf.render().Result;
和
wBmp = await nf.render().ConfigureAwait(false);
我收到 HRESULT: 0x8004C00F 错误在 catch {} 之后
target = await renderer.RenderAsync().AsTask().ConfigureAwait(false);
这是堆栈跟踪:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Nokia.Graphics.Imaging.WriteableBitmapRenderer.<<RenderAsync>b__0>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at SomeApp.NokiaFilters.<render>d__f.MoveNext()