Null SynchronizationContext for some user

Add-in Express™ Support Service
That's what is more important than anything else

Null SynchronizationContext for some user
 
Subscribe
Kiru Marimuthu




Posts: 8
Joined: 2017-07-20
Hi,

I need the current synchronizationcontext in my rtd server to updatetopics. Since the SynchronizationContext.Current is null in the rtd server class, I am getting in the Addin Module like this.

 public AddinModule()
        {

            Context = SynchronizationContext.Current;// It works for me but not for my colleague
        }


Then accessing the context in the rtd server.

Context = AddinModule.Context;
 Context.Post(x => UpdateTopics(), null);


The above code works fine for me, but it doesnt work for my colleague. SynchronizationContext is always null for him.
Any help please?
Posted 04 Oct, 2017 12:03:17 Top
Kiru Marimuthu




Posts: 8
Joined: 2017-07-20
Please ignore this, as it is incorrect. I have posted another question here.

SynchronizationContext.Current is null
Posted 05 Oct, 2017 04:47:33 Top
Andrei Smolin


Add-in Express team


Posts: 14096
Joined: 2006-05-11
Hello Kiru,

Try to get the context in an event of the add-in module, not in the constructor. We suggest using the OnRibbonBeforeCreate or AddinInitialize event because one of them is the very first event that Office sends to your add-in. If any of these events is too late for you, override the ADXAddinModule.OnHostApplicationInitialized(); this is the earliest possible moment. Please let me know if this works for you.

Kiru Marimuthu writes:
Context.Post(x => UpdateTopics(), null);


At https://referencesource.microsoft.com/#mscorlib/system/threading/synchronizationcontext.cs,8b34a86241c7b423, I see the following implementation of the SynchronizationContext.Post() method:

        public virtual void Post(SendOrPostCallback d, Object state)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(d), state);
        }


As you can see, the UpdateTopics() method is executed on a thread other than the main one. We don't recommend doing this in an Office extension. A way to always execute your code on the main thread is to use the SendMessage/OnSendMessage trick that we describe in section Wait a Little at https://www.add-in-express.com/docs/net-office-tips.php#wait-a-little.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 05 Oct, 2017 04:49:05 Top
Kiru Marimuthu




Posts: 8
Joined: 2017-07-20
Thanks for the response. So using
Context.Post(x => UpdateTopics(), null);
is wrong way of updating topics in RTD?
I should be using the SendMessage/OnSendMessage as given in the link.

private const int WM_USER = 0x0400; 
private const int MYMESSAGE = WM_USER + 1000; 
private void adxKeyboardShortcut1_Action(object sender) { 
    this.SendMessage(MYMESSAGE); 
} 
         
private void AddinModule_OnSendMessage(object sender,  
    AddinExpress.MSO.ADXSendMessageEventArgs e) { 
    if (e.Message == MYMESSAGE) { 
        // do your stuff here  
    } 
} 


How do I set "MYMESSAGE" to so it won't clash with existing messages?
Posted 05 Oct, 2017 05:16:59 Top
Andrei Smolin


Add-in Express team


Posts: 14096
Joined: 2006-05-11
Kiru,

To prevent interfering with messages sent from other sources, you can register a message using the RegisterWindowMessage WinAPI function, see http://www.pinvoke.net/default.aspx/user32.registerwindowmessage. The message must be greater than 1024.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 05 Oct, 2017 05:47:32 Top
Gavin Howard




Posts: 6
Joined: 2016-12-01
Thanks for your reply.

Just to confirm, using this methodology will cause us problems in our Excel addin?


Context.Post(x => UpdateTopics(), null); 
Posted 05 Oct, 2017 05:49:36 Top
Andrei Smolin


Add-in Express team


Posts: 14096
Joined: 2006-05-11
Accessing an Office COM interfaces from a background thread may raise an exception if the host application is busy and it doesn't respond within a specified time period. In the general case, you are supposed to intercept that exception and issue the same COM call once again (say, after a delay). In the case of an Office extension (COM add-in or RTD server), doing this complicates the project enormously. This doesn't look practical. Anyway, Office is STA and this means your call will be translated to the main thread, put in a queue, and executed from the queue; that is, using a thread in this case simply introduces an extra overhead; you gain nothing. You can find more details: read what Geoff Darst from the VSTO Team says at http://social.msdn.microsoft.com/forums/en-US/vsto/thread/a4775ced-fa6d-44bf-b039-5bc72188e823.

So, the answer is yes, accessing the Excel object model or RTD server interfaces on a background tread may cause problems.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 05 Oct, 2017 06:31:34 Top