Home > Blockchain >  UWP RenderTargetBitmap.RenderAsync Hangs
UWP RenderTargetBitmap.RenderAsync Hangs

Time:05-18

I wrote a function to rasterize stroke data from an InkCanvas, but for some reason renderBMP.RenderAsync(...) hangs indefinitely. Any advice?

    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;

    }

CodePudding user response:

As Theodor Zoulias said, you synchronous call to async methods make thread stuck, you could use await keyword to repalce task.AsTask().Wait();, please refer to the following code

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;    
}

And for getting more info about Threading and async programming document.

  • Related