使用 STA Thread 或 TaskScheduler.FromCurrentSynchronizationContext() 进行并行编程

发布于 2024-12-28 20:47:38 字数 1874 浏览 1 评论 0原文

我正在编写一个使用 WPF 控件生成 XPS 文件的代码。假设有 100 页的文档。我们知道,由于它使用 WPF UI 元素,因此它必须是 STA 线程。 在前端,我有一个 Web 服务,它调用类库模块中的GenerateBook 方法,生成 100 页 XPS 文件。下面的代码部分工作得非常好。但我想让它使用并行编程更快。所以我用下面的代码转换了代码,但我发现对四核(4 CPU)机器的性能没有影响。 STA 线程是否真的不能并行运行或者我哪里做错了?

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

并行编程后

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

            }

任何回应将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文