Visio.Application.DeferRecalc hangs Visio

Started by bwharrington, September 21, 2018, 08:17:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bwharrington

I have a class that I am using to turn Events On and Off that includes using DefereRecalc.

Below is that class and sample usage below it.

I highlighted the line that causes the hang. Removing it seems to fix my issue. However I'm nervous about removing this. As I researched this it is mainly a performance enhancement.

I found something sort of supporting this may not be a good idea and that you can find unexpected behavior. However from other posts in this forum I see people using this without issue. So it seems :)

http://visguy.com/vgforum/index.php?topic=6813.msg28466#msg28466

I have verified the value I am setting and all appears to be correct.

Has any experienced problems using DefereRecalc and possibilities to resolving them?


public class VisioEventSilencer : IDisposable
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private short _lastEventsEnabledValue;
        private short _lastDeferRecalcValue;
        private Visio.Application _application;

        public VisioEventSilencer(Visio.Application application)
        {
            _application = application;
            Off();
        }

        public VisioEventSilencer Off()
        {
            logger.Debug("Turning events off");
            _lastEventsEnabledValue = _application.EventsEnabled;
            _application.EventsEnabled = VisioHelper.FALSE;
            _lastDeferRecalcValue = _application.DeferRecalc;
            _application.DeferRecalc = VisioHelper.TRUE;

            return this;
        }

        public void Restore()
        {
            logger.Debug("Restoring events");
            try
            {
                _application.EventsEnabled = _lastEventsEnabledValue;
                [color=yellow][color=red]_application.DeferRecalc = _lastDeferRecalcValue;[/color][/color]
            }
            catch(Exception ex)
            {
                logger.Debug(ex.Message);
                logger.Debug(ex.StackTrace);
            }
        }

        #region IDisposable Support
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue) {
                if (disposing) {
                    this.Restore();
                }

                disposedValue = true;
            }
        }

        ~VisioEventSilencer()
        {
            Dispose(false);
        }

        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }


Sample Usage

Application application....
using (var silencer = new VisioEventSilencer(application))
           {
               //Get Server Data, Send Server Data, Create Shapes, Manipulate Existing Shapes and so on.
            }


Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

bwharrington I got Visio internal errors after deferrecalc is set to True (Visio 2019 and later), I would like the speed boost when setting a ton of things at once in code but I need to understand what happens in the Visio engine better to know what can and cannot be done safely while deferrecalc = True.

Visio 2021 Professional

Paul Herber

I suspect that after 4 years he has lost interest!
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

Yes  ;D ;D ;D but I am adding this because it might be valuable for someone else to know. I often use the Visio Guy-forum as a knowledge base, so I love it when people add more relevant info to existing posts & replies!
Visio 2021 Professional