Word 2007 add-in asynchronous operation
I am trying to create a Word add-in (Word 2007, VS 2008) to get various statistics for long texts. The functions for computing statistics are in a separate class and are not complicated, but are time-consuming. The calculations are started from a custom task pane button in my add-in and presented in a DataGridView control in the same CTP when finished. This works well, except that Word is blocked while the calculations are performed. For long texts this can take time (for example ~ 10 seconds for 800 hundred pages document). I have read some previous threads on this issue and attempted to use BackgroundWorker, but this didn't help - Word is stil blocke开发者_JS百科d. Here is my current code:
private void btnAnalyze_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = Tools.CalculateStatistics(
Globals.ThisAddIn.Application.ActiveDocument.Content.Text,
statOptions);
}
private void worker_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
gridResults.BeginInvoke(
new GridDelegate(RefreshGrid),
e.Result as List<Tools.StatResults>);
}
private void RefreshGrid(List<Tools.StatResults> list)
{
statList = list;
gridResults.DataSource = statList;
gridResults.Refresh();
}
worker is the BackgroundWorker object, gridResults is the DataGridView control, statList is a generic List that is a data source for the DataGridView. (it is a private member of my add-in usercontrol)
I haven't already tried using a thread in Word. The last article I've read about related to Outlook, but I think it's worth to have a look at the comment of Ken Slovak concerning threads in Outlook 2010 64 bit Add-in- Crashing creating another thread.
精彩评论