Itextsharp - 多线程问题
Itext Sharp - multithreading issues
: Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.
在线阅读,看起来 iText PDF sharp 是线程安全的(但也许我错了!)。
当 运行 在并行 for 循环中输入文本时,我得到上述异常。
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(path);
StringBuilder text = new StringBuilder();
Parallel.For(1, pdfReader.NumberOfPages + 1, page =>
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
text.Add(currentText);
}
);
pdfReader.Close();
只有一个线程可以同时访问同一个文档。 iText(Sharp) 是线程安全的,因为多个线程可以同时创建和操作不同 个文档。
: Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.
在线阅读,看起来 iText PDF sharp 是线程安全的(但也许我错了!)。
当 运行 在并行 for 循环中输入文本时,我得到上述异常。
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(path);
StringBuilder text = new StringBuilder();
Parallel.For(1, pdfReader.NumberOfPages + 1, page =>
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
text.Add(currentText);
}
);
pdfReader.Close();
只有一个线程可以同时访问同一个文档。 iText(Sharp) 是线程安全的,因为多个线程可以同时创建和操作不同 个文档。