Every once in a while (500 iterations or more) I'm getting an exception in the static method below stating "calling thread cannot access this object because a different thread owns it". Thing is, this method references no external objects
and performs no special threading operations. All of the WPF objects are created and consumed in this method. The only aspect that is multi-threaded is that this method can get called concurrently on different threads in the same app domain (the
project is a Windows service). That said the fileToDecollate parameter will be unique every time, so there is no "collision" as far as that goes.
Any ideas? This is maddening and in theory it should not be possible to blow this error.
Exception Information------------------------------------------
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.GetLocalValueEnumerator()
at System.Windows.Xps.Serialization.SerializersCacheManager.GetTypeDependencyPropertiesCacheItem(Object serializableObject)
at System.Windows.Xps.Serialization.SerializersCacheManager.GetSerializableDependencyProperties(Object serializableObject)
at System.Windows.Xps.Serialization.SerializablePropertyCollection.InitializeSerializableDependencyProperties()
at System.Windows.Xps.Serialization.SerializablePropertyCollection.Initialize(PackageSerializationManager serializationManager, Object targetObject)
at System.Windows.Xps.Serialization.SerializableObjectContext.CreateContext(PackageSerializationManager serializationManager, Object serializableObject, SerializableObjectContext serializableObjectParentContext, SerializablePropertyContext serializablePropertyContext)
at System.Windows.Xps.Serialization.ReachSerializer.DiscoverObjectData(Object serializedObject, SerializablePropertyContext serializedProperty)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.FixedDocumentSerializer.SerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.ReachDocumentReferenceSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.ReachDocumentReferenceCollectionSerializer.SerializeDocumentReference(Object documentReference)
at System.Windows.Xps.Serialization.ReachDocumentReferenceCollectionSerializer.SerializeDocumentReferences(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachDocumentReferenceCollectionSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(SerializablePropertyContext serializedProperty)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeProperty(SerializablePropertyContext serializablePropertyContext)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeProperties(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeObjectCore(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.DocumentSequenceSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
at System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
at System.Windows.Xps.XpsDocumentWriter.Write(FixedDocumentSequence fixedDocumentSequence)
at MyCompany.Utilities.Document.XPSDocument.Decollate(String fileToDecollate, String outputPath) in E:\Projects\MyCompany\Utilities\MyCompany.Utilities.Document\XPSDocument.cs:line 358
at MyCompany.Services.ERM.XPSCapture.Decollate(String fileToDecollate, String outputPath) in E:\Projects\MyCompany\Services\MyCompany.Services.ERM\XPSCapture.cs:line 210
at MyCompany.Services.ERM.ERMFileProcessor.decollateERMFile(String tempERMFile, IFileCapture fileCapture) in E:\Projects\MyCompany\Services\MyCompany.Services.ERM\ERMFileProcessor.cs:line 1257
at MyCompany.Services.ERM.ERMFileProcessor.process() in E:\Projects\doc-link\MyCompany\Services\Altec.Services.ERM\ERMFileProcessor.cs:line 354
at MyCompany.Services.ERM.ERMFileProcessor.Process(ProcessingCompleteCallback callback) in E:\Projects\MyCompany\Services\MyCompany.Services.ERM\ERMFileProcessor.cs:line 90
Additonal Info------------------------------------------
ExceptionManager.MachineName: BTP-30-DEV
ExceptionManager.WindowsIdentity: XXXXX-WA\Bradley
ExceptionManager.FullName: WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
ExceptionManager.AppDomainName: MyCompany.XXXXXXServiceHost.exe
ExceptionManager.ProcessInfo: PID=7392, ThreadID=7816, Managed ThreadID=36, Name=MyCompany.XXXXXXServiceHost, Uptime=00:02:12.2548416
ExceptionManager.ProcessResourceUsage: Working Set=265188 KB, Peak Working Set=525940 KB, Virtual Memory Size=530264 KB, Peak VM Size=809832 KB, Handles=631
ExceptionManager.SystemMemory: Load=81%, Total Physical=3168748 KB, Free Physical=586736 KB, Total PageFile=5213700
publicstaticList<string>
Decollate(stringfileToDecollate,stringoutputPath)
{
// iterate fixed documents and fixed pages, create new XPS files
List<string>
xpsDecFiles = newList<string>();
using(XpsDocumentxpsSourceDocument
= newXpsDocument(fileToDecollate,FileAccess.Read))
{
FixedDocumentSequencefixedDocSeq
= xpsSourceDocument.GetFixedDocumentSequence();
intpageNumber = 0;
foreach(DocumentReferencedocReferenceinfixedDocSeq.References)
{
FixedDocumentsourceFixedDoc
= docReference.GetDocument(false);
foreach(PageContentpageinsourceFixedDoc.Pages)
{
pageNumber++;
// prepare new fixed doc sequence
FixedDocumentSequencenewFixedDocSeq
= newFixedDocumentSequence();
DocumentReferencenewDocReference
= newDocumentReference();
FixedDocumentnewFdoc
= newFixedDocument();
newDocReference.SetDocument(newFdoc);
// copy the page
PageContentnewPage =newPageContent();
newPage.Source = page.Source;
(newPage asIUriContext).BaseUri
= ((IUriContext)page).BaseUri;
// tickle this method... presumably just to load the FixedPage data.
FixedPagenewFixedPage
= newPage.GetPageRoot(false);
// Add page to fixed doc sequence.
newFdoc.Pages.Add(newPage);
// Always do this last: add document reference to fixed doc sequence.
newFixedDocSeq.References.Add(newDocReference);
// create and save new XPS doc
stringdecFileName =Path.Combine(outputPath,Path.GetFileNameWithoutExtension(fileToDecollate)+ "~"+ (pageNumber).ToString()+ FileExtensions.XPS);
if(File.Exists(decFileName))File.Delete(decFileName);
XpsDocumentnewXPSDoc
= newXpsDocument(decFileName,FileAccess.ReadWrite);
// testing. thread IDs should be the same.
//Debug.Assert(System.Windows.Threading.Dispatcher.CurrentDispatcher.Thread.ManagedThreadId == newFixedDocSeq.Dispatcher.Thread.ManagedThreadId);
XpsDocument.CreateXpsDocumentWriter(newXPSDoc).Write(newFixedDocSeq);
newXPSDoc.Close();
// add file to list
xpsDecFiles.Add(decFileName);
}
}
}
returnxpsDecFiles;
}