Dav*_*vid 6 c# parallel-processing multithreading
我开始乱用多线程来处理我正在运行的CPU密集型批处理.基本上我正在尝试将多个单页tiff压缩成单个PDF文档.这适用于foreach循环或标准迭代,但对于几百页文档来说可能非常慢.我尝试了以下基于我发现使用多线程的一些示例,并且它具有显着的性能改进但是它消除了页面顺序而不是1,2,3,4它将是1,3,4,2,6,5 on什么线程首先完成.
我的问题是如何在维护页面顺序的同时利用这种技术,如果可以,它会否定多线程的性能优势?先感谢您.
PdfDocument doc = new PdfDocument();
string mail = textBox1.Text;
string[] split = mail.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
int counter = split.Count();
// Source must be array or IList.
var source = Enumerable.Range(0, 100000).ToArray();
// Partition the entire source array.
var rangePartitioner = Partitioner.Create(0, counter);
double[] results = new double[counter];
// Loop over the partitions in parallel.
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
// Loop over each range element without a delegate invocation.
for (int i = range.Item1; i < range.Item2; i++)
{
f_prime = split[i].Replace(" " , "");
PdfPage page = doc.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(f_prime);
double x = 0;
gfx.DrawImage(image, x, 0);
}
});
Run Code Online (Sandbox Code Playgroud)
我不确定其他解决方案能否完全按照他想要的方式工作。这样做的原因是PdfPage page = doc.AddPage();同时创建和添加一个新页面,因此它总是会乱序,因为顺序是先到先服务的doc
如果AddPage是快速操作,您可以一次创建所有 100 个页面,无需任何处理。然后返回并将 Tiff 图像渲染到页面中。
PdfDocument doc = new PdfDocument();
string mail = textBox1.Text;
string[] split = mail.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
int counter = split.Count();
// Source must be array or IList.
var source = Enumerable.Range(0, 100000).ToArray();
// Partition the entire source array.
var rangePartitioner = Partitioner.Create(0, counter);
double[] results = new double[counter];
PdfPage[] pages = new PdfPage[counter];
for (int i = 0; i < counter; ++i)
{
pages[i] = doc.AddPage();
}
// Loop over the partitions in parallel.
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
// Loop over each range element without a delegate invocation.
for (int i = range.Item1; i < range.Item2; i++)
{
f_prime = split[i].Replace(" " , "");
PdfPage page = pages[i];
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(f_prime);
double x = 0;
gfx.DrawImage(image, x, 0);
}
});
Run Code Online (Sandbox Code Playgroud)
编辑
我认为有一个更优雅的解决方案,但在不知道 PdfPage 属性的情况下,我之前不想提供它。如果您知道 PfdPage 属于哪个页面,则可以使事情变得非常简单,如下所示:
PdfDocument doc = new PdfDocument();
string mail = textBox1.Text;
string[] split = mail.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
int counter = split.Count();
// Source must be array or IList.
var source = Enumerable.Range(0, 100000).ToArray();
// Partition the entire source array.
var rangePartitioner = Partitioner.Create(0, counter);
double[] results = new double[counter];
// Loop over the partitions in parallel.
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
// Loop over each range element without a delegate invocation.
for (int i = range.Item1; i < range.Item2; i++)
{
PdfPage page = doc.AddPage();
// Only use i as a loop not as the index
int pageIndex = page.PageIndex; // This is what I don't know
f_prime = split[pageIndex].Replace(" " , "");
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(f_prime);
double x = 0;
gfx.DrawImage(image, x, 0);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1199 次 |
| 最近记录: |