This project is read-only.

Using Parallel.ForEach with SevenZipExtractor.ExtractFile()

May 21, 2012 at 3:37 AM

I have been looking at ways to speed up our processing of individual items from within zip files and have come across some issues using Parallel.ForEach. What I do is something like this (this is an untested extraction from our program):

private readonly Object zipLockObject = new Object();

:
:

private void ProcessArchiveContentsParallel()
{
   IList<ICompressedFileInfo> compressedFileInfoList = compressedFileHelper.GetFileInfo();

   Parallel.ForEach(compressedFileInfoList, compressedFile =>
	{
		var tempFilename = FileUtils.GetTempFileName(tempPath, Thread.CurrentThread.ManagedThreadId + "-" + compressedFile.Name);
	    	    
		lock (zipLockObject)
		{
			using (var stream = File.Create(tempFilename))
			{
				extractor.ExtractFile(compressedFile.Index, stream);
			}
		}
		
		ProcessFile(tempFilename);		
	}
}

However when I run this I get the following:

System.InvalidCastException was unhandled by user code
  HResult=-2147467262
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'SevenZip.IInArchive'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{23170F69-40C1-278A-0000-000600600000}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  Source=mscorlib
  StackTrace:
       at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
       at SevenZip.IInArchive.Extract(UInt32[] indexes, UInt32 numItems, Int32 testMode, IArchiveExtractCallback extractCallback)
       at SevenZip.SevenZipExtractor.ExtractFile(Int32 index, Stream stream)
       :

Is this expected behaviour?

Jun 29, 2012 at 2:59 AM
Edited Jun 29, 2012 at 2:59 AM

Update. This happens when the program has the STAThread attribute. i.e.

internal class Program
{
	[STAThread]
	private static void Main(string[] args)
	{   ...
Jul 2, 2012 at 4:59 AM

This may shed some light on what is going on: http://social.msdn.microsoft.com/Forums/sv/parallelextensions/thread/7d4c66fd-e716-41ec-8b4d-2620400a0150