locked
hang in RenderToStreamAsync() RRS feed

  • Question

  • Hi,

    I am using this PDF API for view the PDF files. I rendered PDF file as a image using 
    await _pdfPage.RenderToStreamAsync(randomStream, option);
    imgSource.SetSource(randomStream);

    When i call that method more than 170 times it gets hang.

    How do we solve that?


    Thanks by Manivel

    Wednesday, December 4, 2013 2:45 PM

All replies

  • Don't call it 170 times!  :-).

    Seriously though, you have not given any useful information about how to create the problem or the code surrounding this.  It is impossible to know for certain what the problem is.  You most likely are not closing or disposing of a stream handle.  Please post a complete simple repro of the problem and steps to cause the problem to SkyDrive and someone could assist you!

    Thanks!

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, December 4, 2013 8:22 PM
    Moderator
  • Hi Jeff,

    public async Task<BitmapImage> TriggerInitialize(PdfPage _pdfPage, double zoomFactor, double height)
            { 
    	    BitmapImage img = new BitmapImage();
                if (_pdfPage != null)
                {                
                    try
                    {                  
                        cts.Cancel();
                        cts = new System.Threading.CancellationTokenSource();
                        //actual page Image
                        InMemoryRandomAccessStream randomStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
                        PdfPageRenderOptions option = new PdfPageRenderOptions();                   
                        if (height != 0)
                        {
                            _height = height;
                            _width = (int)((_pdfPage0.Size.Width / _pdfPage0.Size.Height) * height);                       
                        }
                        else
                        {
                            _height = (int)PdfPage.Size.Height;
                            _width = (int)PdfPage.Size.Width;
                        }
                        if (_height > 0)
                        {
                            option.DestinationHeight = (uint)(_pdfPage0.Size.Height * zoomFactor);
                        }
                        if (_width > 0)
                        {
                            option.DestinationWidth = (uint)(_pdfPage0.Size.Width * zoomFactor);
                        }                  
                        await _pdfPage.RenderToStreamAsync(randomStream, option);                                           
                        img.SetSource(randomStream);                 
                        randomStream.Dispose();                    
                        GC.Collect();
                    }
                    catch (Exception e)
                    {
                        string msg = e.Message;                    
                    }               
                }            
                return img;
            }

    Scenario: I am downloading PDF pages and bind into flip view with two pages in a single item. I am downloading more  than 200 pages and convert as images and bind into Flip view. the above code which is using to convert image. When i call  await _pdfPage.RenderToStreamAsync(randomStream, option) method at 171th page it won't return any value and debug control does not go to next line. Again I go to other page and come back to that same page  (_pdfDocument = await docLoadTask.AsTask<PdfDocument>(cts_cover.Token); ) this is line not return any and debug does not go to next line.

     var dataUri = new Uri("ms-appx:///Assets/PDF/1.pdf");
                    var file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                    var docLoadTask = PdfDocument.LoadFromFileAsync(file);
                    _pdfDocument = await docLoadTask.AsTask<PdfDocument>(cts_cover.Token);
                    PdfPage p = _pdfDocument.GetPage(0);


    Thanks by Manivel


    • Edited by Manivelp Thursday, December 5, 2013 9:01 AM
    Thursday, December 5, 2013 4:29 AM
  • Hi Manivel,

    I would skip forcing the GC to collect.  Also when you have streams use the 'using' functionality like this and don't bother with the .Dispose (using takes care of that):

    using(varstream = newWindows.Storage.Streams.InMemoryRandomAccessStream())
      {

    ...Code here


    }


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, December 5, 2013 1:56 PM
    Moderator
  • Hi Jeff,

    Thanks to reply. I applied but my issue is not solved. Again i got the same issue when it reached 171st page loading.


    Thanks by Manivel

    Friday, December 6, 2013 6:05 AM
  • Hi Manivel,

    I am sorry to hear that.  Please post a complete simple repro of the problem and steps to cause the problem to SkyDrive and someone could assist you!  Or alternatively you can open a support case for personal help with your code.

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Friday, December 6, 2013 1:13 PM
    Moderator