I am trying to use the Alternet SyntaxEdit in a VSTO Excel addin. When used outside the addin all works fine, but hen I call up the edit form in the addin after a few seconds I get a cross threading exception.
Here is a link to a solution with three projects:
- AlternetEditor - a simple winforms library with a form with the syntax editor on it
- AlterNetTest - A simple winforms executable that references AlterNetEditor and shows the form. This works well
- An simple Excel Addin that has a ribbon with a button to show a simple task pane. The task pane has a single button that when clicked brings up the AlterNetEditor form. It crashes after a second with cross thread exception
This is related to a known issue with Office not setting a synchronization context for the async operations to be continued on the UI thread.
As a workaround, one has to manually set a Windows Forms synchronization context at the beginning of every
async void method.
See the following links for more information:
For this case, in order to fix the problem please apply the following changes to the frmTest.cs file:
public MyJSParser(ITypeScriptProject project) : base(project)
public override void ReparseText(bool useThread, int first, int last)
base.ReparseText(useThread, first, last);
Thanks Yevgeni, So I put in the changes you gave above. It now does show the edit form in Excel, but I notice that if I hover the mouse over know variables, like row, or rows in the example it does not recognize them. I also saw that if I type in row. for example, it does not pop up a completion window. I then typed control + space and I get the cross thread exception!
I attach the updated project in case I missed something. The issue reproduces each time in Excel, but does work outside.
To fix this, please add the following method to your custom parser class:
public override void CodeCompletion(string text, StringItemInfo textData, Point position, bool useThread, CodeCompletionArgs e)
base.CodeCompletion(text, textData, position, useThread, e);
As the sources from my previous reply note, there is no way to fix this for all async calls globally in an Office add-in. So we need to set the
WindowsFormsSynchronizationContext at the beginning of any
async void event handler. The
CodeCompletion method above is one such place.
Ah ok thanks Yevgeni. Can you give me a list of the main methods I need to override so the tooltips work etc. Thanks!
Ok I tested and it now seems to do all that I need. In case though if there are any other methods I can override to make the editing experience complete please let me know!
Agian thanks for the super speedy response!!!