Click here to Skip to main content
15,881,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been trying to turn the code sample at http://code.msdn.microsoft.com/office/CSVSTOViewWordInWPF-db347436[^] into an asynchronous program.

I've been running into issues where running the program by calling AssignDocument(w,x) using await causes it to run synchronously instead of asynchronously.

Using Factory.StartNew causes an error stating "A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll".

Also, do I have to pass a dummy variable like Task<bool> to make this work?

C#
private async void btnViewDoc_Click(object sender, RoutedEventArgs e)
{
   string wordDocument =txbSelectedWordFile.Text;
   if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
   {
      MessageBox.Show("The file is invalid. Please select an existing file again.");
   }
   else
   {
      string convertedXpsDoc = string.Concat(Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");
      //await AssignDocument(wordDocument, convertedXpsDoc);
      //System.Threading.Tasks.Task MyNewTask = System.Threading.Tasks.Task.Factory.StartNew(() => AssignDocument(wordDocument, convertedXpsDoc));
   }
}

private async Task<bool> AssignDocument(string wordDocumentURI, string xpsDocURI)
{
   XpsDocument xpsDocument = ConvertWordToXps(wordDocumentURI, xpsDocURI);
   if (xpsDocument == null)
   {
      return false;
   }
   else
   {
      documentviewWord.Document = xpsDocument.GetFixedDocumentSequence();
      return true;
   }
}
Posted
Updated 30-Jun-14 8:41am
v3
Comments
Kyle Gottfried 30-Jun-14 17:20pm    
New code, still running into issues.

<pre lang="c#">private async void btnViewDoc_Click(object sender, RoutedEventArgs e)
{
string wordDocument =txbSelectedWordFile.Text;
if (string.IsNullOrEmpty(wordDocument) || !File.Exists(wordDocument))
{
MessageBox.Show("The file is invalid. Please select an existing file again.");
}
else
{
string convertedXpsDoc = string.Concat(Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");
//await AssignDocument(wordDocument, convertedXpsDoc);
SysTask.Task MyNewTask = await SysTask.Task.Factory.StartNew(() => AssignDocument(wordDocument, convertedXpsDoc));
//documentviewWord.Document = ConvertWordToXps(wordDocument, convertedXpsDoc).GetFixedDocumentSequence();
//AssignDocument(wordDocument, convertedXpsDoc);
Thread thread = new Thread(new ThreadStart(() => AssignDocument(wordDocument, convertedXpsDoc)))
{
ApartmentState = ApartmentState.STA
};
}
}

private async SysTask.Task AssignDocument(string wordDocumentURI, string xpsDocURI)
{
XpsDocument xpsDocument = ConvertWordToXps(wordDocumentURI, xpsDocURI);
if (xpsDocument != null)
documentviewWord.Document = xpsDocument.GetFixedDocumentSequence();
}
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900