1

I have a class with a method (CreateDocument) that fires an event at the end. The event args contain a FixedDocument. In my MainWindow code I try to set a DocumentViewer's Document like:

void lpage_DocCreated(object sender, LabelDocumentEventArgs e) { this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(delegate { FixedDocument fd = e.doc; documentViewer1.Document = fd; documentViewer1.FitToWidth(); return null; }), null); } 

I receive "The calling thread cannot access this object because a different thread owns it." on line documentViewer1.Document = fd;

I am able to update a progress bar in another event handler that the same method fires while it is working:

 Int32 progress = Int32.Parse(sender.ToString()); progBar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate { progBar.Value = progress; return null; }), null); 

I can't figure out why I can't set the document when I'm essentially doing the same type of thing when I set the progress bar value.

1 Answer 1

0

The FixedDocument element also has thread-affinity. So if you are creating it in a separate thread than the documentViewer1, then you would get that exception.

Basically, anything that derives from DispatcherObject has a thread-affinity. FixedDocument derives from DispatcherObject, just like the viewer controls.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.