UWP RenderTargetBitmap.RenderAsync 挂起
UWP RenderTargetBitmap.RenderAsync Hangs
我编写了一个函数来栅格化来自 InkCanvas 的笔划数据,但由于某种原因 renderBMP.RenderAsync(...)
无限期挂起。有什么建议吗?
RenderTargetBitmap rasterizePage(int pageNumber) {
if (readStrokes(pageNumber) == null) return null;
InkCanvas virtualInkCanvas = new InkCanvas();
virtualInkCanvas.Width = MainGrid.ActualWidth / 2;
virtualInkCanvas.Height = MainGrid.ActualHeight;
foreach (InkStroke stroke in readStrokes(pageNumber))
{
virtualInkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke.Clone());
}
RenderTargetBitmap renderBMP = new RenderTargetBitmap();
System.Diagnostics.Debug.WriteLine("Start Rendering page " + pageNumber.ToString());
var task = renderBMP.RenderAsync(virtualInkCanvas, (int)(MainGrid.ActualWidth / 2), (int)MainGrid.ActualHeight);
task.AsTask().Wait();
System.Diagnostics.Debug.WriteLine("Done Rendering page " + pageNumber.ToString());
return renderBMP;
}
正如Theodor Zoulias所说,你同步调用async方法会导致线程卡住,你可以使用await关键字来替换task.AsTask().Wait();
,请参考下面的代码
public async Task<RenderTargetBitmap> rasterizePage(int pageNumber)
{
if (readStrokes(pageNumber) == null) return null;
InkCanvas virtualInkCanvas = new InkCanvas();
virtualInkCanvas.Width = MainGrid.ActualWidth / 2;
virtualInkCanvas.Height = MainGrid.ActualHeight;
foreach (InkStroke stroke in readStrokes(pageNumber))
{
virtualInkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke.Clone());
}
RenderTargetBitmap renderBMP = new RenderTargetBitmap();
System.Diagnostics.Debug.WriteLine("Start Rendering page " + pageNumber.ToString());
await renderBMP.RenderAsync(virtualInkCanvas, (int)(MainGrid.ActualWidth / 2), (int)MainGrid.ActualHeight);
System.Diagnostics.Debug.WriteLine("Done Rendering page " + pageNumber.ToString());
return renderBMP;
}
并获取有关 Threading and async programming document 的更多信息。
我编写了一个函数来栅格化来自 InkCanvas 的笔划数据,但由于某种原因 renderBMP.RenderAsync(...)
无限期挂起。有什么建议吗?
RenderTargetBitmap rasterizePage(int pageNumber) {
if (readStrokes(pageNumber) == null) return null;
InkCanvas virtualInkCanvas = new InkCanvas();
virtualInkCanvas.Width = MainGrid.ActualWidth / 2;
virtualInkCanvas.Height = MainGrid.ActualHeight;
foreach (InkStroke stroke in readStrokes(pageNumber))
{
virtualInkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke.Clone());
}
RenderTargetBitmap renderBMP = new RenderTargetBitmap();
System.Diagnostics.Debug.WriteLine("Start Rendering page " + pageNumber.ToString());
var task = renderBMP.RenderAsync(virtualInkCanvas, (int)(MainGrid.ActualWidth / 2), (int)MainGrid.ActualHeight);
task.AsTask().Wait();
System.Diagnostics.Debug.WriteLine("Done Rendering page " + pageNumber.ToString());
return renderBMP;
}
正如Theodor Zoulias所说,你同步调用async方法会导致线程卡住,你可以使用await关键字来替换task.AsTask().Wait();
,请参考下面的代码
public async Task<RenderTargetBitmap> rasterizePage(int pageNumber)
{
if (readStrokes(pageNumber) == null) return null;
InkCanvas virtualInkCanvas = new InkCanvas();
virtualInkCanvas.Width = MainGrid.ActualWidth / 2;
virtualInkCanvas.Height = MainGrid.ActualHeight;
foreach (InkStroke stroke in readStrokes(pageNumber))
{
virtualInkCanvas.InkPresenter.StrokeContainer.AddStroke(stroke.Clone());
}
RenderTargetBitmap renderBMP = new RenderTargetBitmap();
System.Diagnostics.Debug.WriteLine("Start Rendering page " + pageNumber.ToString());
await renderBMP.RenderAsync(virtualInkCanvas, (int)(MainGrid.ActualWidth / 2), (int)MainGrid.ActualHeight);
System.Diagnostics.Debug.WriteLine("Done Rendering page " + pageNumber.ToString());
return renderBMP;
}
并获取有关 Threading and async programming document 的更多信息。