Cross threading exception when using Alternet editor in an Excel VSTO addin

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:

  1. AlternetEditor - a simple winforms library with a form with the syntax editor on it
  2. AlterNetTest - A simple winforms executable that references AlterNetEditor and shows the form. This works well
  3. 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:
https://stackoverflow.com/a/45598390/71689
https://stackoverflow.com/a/32866156/71689
https://blog.stephencleary.com/2011/02/synchronizationcontext-odds-and-ends.html

For this case, in order to fix the problem please apply the following changes to the frmTest.cs file:

class MyJSParser : Alternet.Syntax.Parsers.TypeScript.JavaScriptParser
{
	public MyJSParser(ITypeScriptProject project) : base(project)
	{
	}

	public override void ReparseText(bool useThread, int first, int last)
	{
		SynchronizationContext.SetSynchronizationContext(new System.Windows.Forms.WindowsFormsSynchronizationContext());
		base.ReparseText(useThread, first, last);
	}
}


public static Alternet.Syntax.Parsers.TypeScript.JavaScriptParser GetJSParser(Dictionary<string, object> values)
{
	// var javaScriptParser = new Alternet.Syntax.Parsers.TypeScript.JavaScriptParser(new Alternet.Common.TypeScript.TypeScriptProject());
	var javaScriptParser = new MyJSParser(new Alternet.Common.TypeScript.TypeScriptProject());

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.

Hi,

To fix this, please add the following method to your custom parser class:

class MyJSParser : Alternet.Syntax.Parsers.TypeScript.JavaScriptParser
{
    // ...

	public override void CodeCompletion(string text, StringItemInfo[] textData, Point position, bool useThread, CodeCompletionArgs e)
	{
		SynchronizationContext.SetSynchronizationContext(new System.Windows.Forms.WindowsFormsSynchronizationContext());
		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!!!