1

Closed

RunMacroSynchronization Timer is not stopped by SyncMacro

description

RunMacroSynchronization sets a timer when it's unable to run the SyncMacro but unfortunately there's an off-by-one error and the timer doesn't ever get killed:

It hangs excel and goes in an infinite loop, no exit point from Excel DNA to stop calling it.

Setting the timer (will always pass a timer ID of 0):
            bool runOK = COMRunMacro(_syncMacroName);
            if (!runOK && _timerId == 0)
            {
                // Timer not yet set - start it
                // Timer will be stopped when SyncMacro actually runs.
                IntPtr result = SetTimer(new HandleRef(_syncWindow, _syncWindow.Handle), timerId++, RETRY_INTERVAL_MS, IntPtr.Zero);
                if (result == IntPtr.Zero)
                {
                    // TODO: Handle unexpected error in setting timer
                    Debug.Print("SynchronizationWindow timer could not be set.");
                }
            }
Killing the timer (will always pass a timer ID of 1):
        // Check for timer and disable
        if (_timerId != 0)
        {
            KillTimer(new HandleRef(_syncWindow, _syncWindow.Handle), _timerId);
            _timerId = 0;
        }
Closed Sep 30, 2015 at 9:32 PM by govert
Old problem already fixed.

comments

govert wrote Sep 17, 2015 at 8:00 AM

The issue you raise here has been resolved in v0.33 - the code you show here is not from the current version.
See http://exceldna.codeplex.com/SourceControl/latest#Source/ExcelDna.Integration/ExcelSynchronizationContext.cs for the current version of this file.

There might be another problem, here or elsewhere, but I don't think it is related to this old issue.

shrans007 wrote Sep 17, 2015 at 8:13 AM

Sorry , i copied the code from previous post.

yes i'm using the v0.33 code

bool runOK = COMRunMacro(_syncMacroName);
            if (!runOK && _timerId == 0)
            {
                // Timer is not yet set - so set a new timer
                // The Timer will be stopped when SyncMacro actually runs.
                _timerId = 1; // We're always on the main thread, so no race condition between checking and setting the id/flag
                IntPtr result = SetTimer(new HandleRef(_syncWindow, _syncWindow.Handle), _timerId, RETRY_INTERVAL_MS, IntPtr.Zero);
                if (result == IntPtr.Zero)
                {
                    // TODO: Handle unexpected error in setting timer
                    Debug.Print("SynchronizationWindow timer could not be set.");
                }
            }
Also sent an email to govert@icon.co.za for brief description :

Please Advise if i am doing something wrong.