Code Red > .NET

DocumentBecameCurrent firing more than once, maybe it had too much coffee?

(1/2) > >>

Atook:
I'm trying to catch the DocumentBecameCurrent event so I can check when a user switches open drawings and do things like update palettes with drawing info, add command handlers etc.
The problem I'm having is that the eventhandler is firing 3 times every time the active drawing is changed (via tabs). I want it to fire just once, feels like I'm missing something obvious...

Here's the code in my IExtensionApplication:

--- Code - C#: ---#if BRX_APPusing Teigha.DatabaseServices;using Teigha.Runtime;using Bricscad.ApplicationServices;using Bricscad.Windows;using _AcadApp = Bricscad.ApplicationServices.Application;using _Ribbon = Bricscad.Ribbon;#elif ACAD_APPusing Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.Windows;using _AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;using _Ribbon = Autodesk.AutoCAD.Ribbon;#endif public void Initialize(){  // does this mean every time a document becomes current we add another EventHandler?  docManager.DocumentBecameCurrent+=new DocumentCollectionEventHandler(UpdateUI);} private void UpdateUI(object sender, DocumentCollectionEventArgs documentCollectionEventArgs){  Document doc = _AcadApp.DocumentManager.MdiActiveDocument;  string name = doc.Database.Filename;  Debug.WriteLine($"Document Made Active: {name}"); //<---Fires 3 times?  // Show our palette if our objects are found in the drawing.  // maybe import needed blocks/styles}

Jeff_M:
Have you tried the DocumentActivated event instead? Also, at one time we had a problem with the MdiActiveDocument not being correct during the event. So we changed to using the eventArgs.Document property.

That being said, I have no idea if this would work with Bricscad as I've never used or programmed for it.

MickD:
It's not uncommon for events to be fired more than once as events can cause ripple effects that fire the same event again.

You could set an event flag that you set after doing your thing then at the end of the command/routine set the flag to false ready for next event perhaps?

It's Alive!:
maybe you can store the doc... something like


--- Code - C#: ---    public  class Commands : IExtensionApplication    {        static private Document m_lastdoc = null;        static private AcAp.DocumentCollection m_docMan = null;         public void Initialize()        {            m_docMan = AcAp.Application.DocumentManager;            m_docMan.DocumentBecameCurrent += new DocumentCollectionEventHandler(UpdateUI);            m_lastdoc = m_docMan.MdiActiveDocument;        }         public void Terminate() { }         private static void UpdateUI(object sender, DocumentCollectionEventArgs documentCollectionEventArgs)        {            Document doc = documentCollectionEventArgs.Document;            if (doc != m_lastdoc)            {                m_lastdoc = doc;                doc.Editor.WriteMessage("\nDocumentBecameCurrent {0}", doc.Name);            }        }    } 

Atook:
Thanks for the input guys. I've switched to the DocumentActivated event, and it seems to be behaving properly. Interesting note, the DocumentActivated event also appears to fire when an autosave happens, and when it does, the MdiActiveDocument.name is the autosave file (*.sv$)

Jeff, that's an interesting problem you were having with MdiActiveDocument. I haven't noticed this issue, but I have had some recent eNotFromThisDocument errors that might be caused from it.

Navigation

[0] Message Index

[#] Next page

Go to full version