1

I'm writing a code which produces the XPS file using WPF controls. Let's say 100 page document. As we know that since its using the WPF UI Elements it must be STA thread. Form the front end I have a web service which call the GenerateBook method in the class library module which generates the 100 page XPS file. The following section of code is working absolutely fine. but What I would like to make it the faster using the parallel programming. so I had converted the code with the below one but I have seen not effect on performance on quad core (4 CPU) machine. Is it true that the STA threads cannot be make run parallel or Where I'm doing wrong?

 for (int pageNumber = from; pageNumber <= to; pageNumber++)
            {
                var pageVisual = paginator.GetPage(pageNumber - 1);
                var page = new PageContent();

                var fixedPage = new FixedPage();

                fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width;
                fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height;

                fixedPage.Children.Add(pageVisual);

                ((IAddChild)page).AddChild(fixedPage);
                fixedDoc.Pages.Add(page);
            }
           ...

after Parallel Programming

TaskScheduler taskUI = TaskScheduler.FromCurrentSynchronizationContext();

            List<Task> tasks = new List<Task>();
            for (int pageNumber = from; pageNumber <= to; pageNumber++)
            {

                int pageCounter = pageNumber;
                var t = Task.Factory.StartNew(() =>
                {
                    var pageVisual = paginator.GetPage(pageCounter - 1);
                    var page = new PageContent();

                    var fixedPage = new FixedPage();

                    fixedPage.Width = fixedDoc.DocumentPaginator.PageSize.Width;
                    fixedPage.Height = fixedDoc.DocumentPaginator.PageSize.Height;

                    fixedPage.Children.Add(pageVisual);

                    ((IAddChild)page).AddChild(fixedPage);
                    fixedDoc.Pages.Add(page);
                }, CancellationToken.None, TaskCreationOptions.None, taskUI);

                tasks.Add(t);

            }

Any response would be much appreciated.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Abdullah
  • 39
  • 6
  • Please [read this answer](http://stackoverflow.com/a/127340/793911). TL;DR: No, I don't think you can have multiple (true) threads in a Single Threaded Apartment model. Your tasks (as I understand it) are being served serially. – zrslv Jan 26 '12 at 13:55
  • Thanks for the link. I understand the STA and MTA. My question is that I'm not using the COM instead its a .NET 4 managed code (WPF). Can I replace the first call (Thread) to Task? Would that work or not? – Abdullah Jan 26 '12 at 14:22
  • I was answering your question "Is it true that the STA threads cannot be make run parallel" :) As far as I know, you cannot use GUI objects outside STA. At least no one can guarantee it will definitely work and remain stable. If that makes any sense (and seems worth the effort) you can try extracting non-WPF code into a separate thread(s) and make it communicate with the GUI one. Sorry, have no idea if there is a better way (which is why I'm not posting this as an answer). – zrslv Jan 26 '12 at 14:31
  • Ah, wait, were you asking if you can have multiple STA threads? Sorry, I've probably got you wrong. – zrslv Jan 26 '12 at 14:37
  • Yes, That's what I mean that If I can create multiple STAthreads (Tasks) for parallelism or not? – Abdullah Jan 26 '12 at 15:36
  • 1
    You can have multiple STA threads, but (AFAIK again, sorry) you cannot rely on UI objects interacting with each other between different threads (in other words, WPF UI is not guaranteed to be thread safe). You can use some communication mechanism which is not relying on living in an STA thread though. – zrslv Jan 26 '12 at 18:04
  • Your code (as it is) tries to access `paginator` from different threads. – zrslv Jan 26 '12 at 18:10

0 Answers0