I am using a BackGroundWorker to handle the parsing of my xml file and
update a FlowDocument.
I am having difficulty getting the FlowDocument from my backgroundworker.
One should be able to create a FlowDocument and pass it as a parameter to my
worker thread.
DocummentParser.RunWorkerAsync(new FlowDocument());
Can use
public void DoWorkHandler(object s, DoWorkEventArgs args)
{
var flowDocument = args.Argument as FlowDocument;
if(flowDocument == null)
throw new Exception("FlowDocument not passed to worker
thread");
ProcessDocument(flowDocument);
args.Result = flowDocument;
}
The updated FlowDocument should be obtained in the completed handler
public void RunWorkerCompletedHandler(object s,
RunWorkerCompletedEventArgs args)
{
LogDocument = (FlowDocument) args.Result;
}
I tried creating the FlowDocument in my thread and then setting args.Result
to this.
However I get an exception
System.Reflection.TargetInvocationException was unhandled
Message=An exception occurred during the operation, making the result
invalid. Check InnerException for exception details.
With the inner exception
the calling thread cannot access this object because a different thread owns
it.
I then tried creating the FlowDocument in my thread and then setting
args.Result to this after it had been worked on.
How can I get my worker thread to build my FlowDocument and then my primary
thread get the completed FlowDocument.
Regards Peter