While I was refactoring some old C# code for document generation with Office.Interop
library I found this and because of it was using UI context when function were called from it it was blocking it
Example
private void btnFooClick(object sender, EventArgs e)
{
bool documentGenerated = chckBox.Checked ? updateDoc() : newDoc();
if(documentGenerated){
//do something
}
}
Decided to change it like that to reduce from blocking UI
private async void btnFooClick(object sender, EventArgs e)
{
bool documentGenerated; = chckBox.Checked ? updateDoc() : newDoc();
if(chckBox.Checked)
{
documentGenerated = await Task.Run(() => updateDoc()).ConfigureAwait(false);
}
else
{
documentGenerated = await Task.Run(() => newDoc()).ConfigureAwait(false);
}
if(documentGenerated){
//do something
}
}
And it was throwing such error
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made
Why does it happen and what is possible workaround?