How can I get and wait for external process to complete?

90 Views Asked by At

I have rewritten a paginator to display header and footer for my documents. Everything seems to work until I save it as xps Document :

        public void SaveAsXps(string fileName, FlowDocument document, string DocumentTitle, string DocumentFooter)
    {
        document.PageHeight = 1122.5 - 30;
        document.PageWidth = 793.7 - 30;

        using (Package container = Package.Open(fileName + ".xps", FileMode.Create))
        {
            using (XpsDocument xpsDoc = new XpsDocument(container, CompressionOption.Maximum))
            {
                XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);

                DocumentPaginator paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;

                paginator = new VisualPaginator(paginator, new Size(793.7, 1122.5), new Size(30, 30), DocumentTitle, DocumentFooter);
                rsm.SaveAsXaml(paginator);
            }
        }

}

I give you the paginator :

 public class VisualPaginator : DocumentPaginator
{

    string m_DocumentTitle;
    string m_DocumentFooter;
    private Size pageSize;
    private Size margin;
    private readonly DocumentPaginator paginator;
    private Typeface typeface;

    public override Size PageSize
    {
        get { return pageSize; }
        set { pageSize = value; }
    }
    public override bool IsPageCountValid
    {
        get
        {
            return paginator.IsPageCountValid;
        }
    }
    public override int PageCount
    {
        get
        {
            return paginator.PageCount;
        }
    }
    public override IDocumentPaginatorSource Source
    {
        get
        {
            return paginator.Source;
        }
    }

    public VisualPaginator(DocumentPaginator paginator, Size pageSize, Size margin, string DocumentTitle, string DocumentFooter)
    {
        PageSize = pageSize;
        this.margin = margin;
        this.paginator = paginator;
        m_DocumentTitle = DocumentTitle;
        m_DocumentFooter = DocumentFooter;

        this.paginator.PageSize = new Size(PageSize.Width - margin.Width * 2,
                                        PageSize.Height - margin.Height * 2);
    }

    public void DrawFunction(DrawingVisual content, string drawContent, Point point)
    {
        try
        {
            using (DrawingContext ctx = content.RenderOpen())
            {
                if (typeface == null)
                {
                    typeface = new Typeface("Times New Roman");
                }

                FormattedText text = new FormattedText(drawContent, CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight, typeface, 14, Brushes.Black,
                    VisualTreeHelper.GetDpi(content).PixelsPerDip);

                Thread.Sleep(300);

                ctx.DrawText(text, point);
            }
        }
        catch (Exception)
        {
            throw;
        }
    }


    public override DocumentPage GetPage(int pageNumber)
    {

        DocumentPage page = paginator.GetPage(pageNumber);

        // Create a wrapper visual for transformation and add extras
        ContainerVisual newpage = new ContainerVisual();
        //Title
        DrawingVisual pagetitle = new DrawingVisual();

        DrawFunction(pagetitle, m_DocumentTitle, new Point(paginator.PageSize.Width / 2 - 100, -96 / 4));
                  
        //Page Number
        DrawingVisual pagenumber = new DrawingVisual();

        DrawFunction(pagenumber, "Page " + (pageNumber + 1), new Point(paginator.PageSize.Width - 200, paginator.PageSize.Height - 100));
                   
        //Footer
        DrawingVisual pagefooter = new DrawingVisual();

        DrawFunction(pagefooter, m_DocumentFooter, new Point(paginator.PageSize.Width / 2 - 100, paginator.PageSize.Height - 100));

        DrawingVisual background = new DrawingVisual();

        using (DrawingContext ctx = background.RenderOpen())
        {
            ctx.DrawRectangle(new SolidColorBrush(Color.FromRgb(240, 240, 240)), null, page.ContentBox);
        }

        newpage.Children.Add(background); // Scale down page and center

        ContainerVisual smallerPage = new ContainerVisual();
        smallerPage.Children.Add(page.Visual);
        //smallerPage.Transform = new MatrixTransform(0.95, 0, 0, 0.95,
        //    0.025 * page.ContentBox.Width, 0.025 * page.ContentBox.Height);

        newpage.Children.Add(smallerPage);
        newpage.Children.Add(pagetitle);
        newpage.Children.Add(pagenumber);
        newpage.Children.Add(pagefooter);

        newpage.Transform = new TranslateTransform(margin.Width, margin.Height);

        return new DocumentPage(newpage, PageSize, page.BleedBox, page.ContentBox);
    }
}

The code execution leads to a break point due to external process unless I use a Thread.Sleep(300) instruction. I think the program has to wait for some external process to complete but I have no Idea what processes are involve here and what I can do to wait for them to fix the problem without using a Thread.Sleep() which is a very bad practice.

Any help or clues would be gladly appreciate.

0

There are 0 best solutions below