使用 STA Thread 或 TaskScheduler.FromCurrentSynchronizationContext() 进行并行编程
我正在编写一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论