Author Topic: Query for EventHandlers  (Read 1164 times)

0 Members and 1 Guest are viewing this topic.

Offline frits432

  • Newbie
  • *
  • Posts: 15
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Query for EventHandlers
« on: November 16, 2011, 09:29:53 AM »
While experimenting with events i noticed that you can add the same handler more than once but found no easy way to prevent this, other than define static variables remembering events already enabled.
Such as
Code: [Select]
public static class Class1
    {
        private bool eventsEnabled;
         public void EnableEvents()
         {
             if (!eventsEnabled)
             {
                 LayoutManager.Current.LayoutSwitched += new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
                 eventsEnabled = true;
...

Is it possible to Query for already enabled Events, like

if (!LayoutManager.Current.LayoutSwitched.Has(OnLayoutSwitched))
LayoutManager.Current.LayoutSwitched += ...

Thanks for any help.


Frits

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: Query for EventHandlers
« Reply #1 on: November 17, 2011, 09:29:06 PM »
I don't think it is possible.
By my side, I sometimes do it like this:
Code: [Select]
try
{
     LayoutManager.Current.LayoutSwitched -= new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
}
catch{}
LayoutManager.Current.LayoutSwitched += new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);

Offline frits432

  • Newbie
  • *
  • Posts: 15
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Query for EventHandlers
« Reply #2 on: November 21, 2011, 09:20:27 AM »
Hi Gile,
Thanks you for replying and sorry for my late responding.
While experimenting i noticed that i can remove the eventhandler without errors even if it is not defined.
So for the code below I can always call REMEV as much as i like without calling ADDEV.
The line "\nFailed to remove eventhandler" will never be executed
So i think the only solution is to add a variable eventsEnabled to prevent duplicated calls to ADDEV

Gr,
Frits

However everytime you call ADDEV, new (duplicated) eventhandlers will be added.
So

Code: [Select]
        [CommandMethod("AddEv")]
        public void AddEvents()
        {
            LayoutManager layMgr = LayoutManager.Current;
            layMgr.LayoutSwitched += new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);

        }

        [CommandMethod("RemEv")]
        public void RemoveEvents()
        {
            try
            {
                LayoutManager layMgr = LayoutManager.Current;
                layMgr.LayoutSwitched -= new Autodesk.AutoCAD.DatabaseServices.LayoutEventHandler(OnLayoutSwitched);
            }
            catch
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                ed.WriteMessage("\nFailed to remove eventhandler");
            }

        }

       public void OnLayoutSwitched(object senderObj, Autodesk.AutoCAD.DatabaseServices.LayoutEventArgs layoutEventArgs)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            ed.WriteMessage("\nOnLayoutSwitched={0}", layoutEventArgs.Name);
        }
Frits

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: Query for EventHandlers
« Reply #3 on: November 21, 2011, 01:08:37 PM »
Hi,

Good catch, the try/catch statement is unuseful.
Thanks.

So it' possible to systematically remove the handler befor adding it.

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

namespace EventHandlerHandling
{
    public class Commands
    {
        [CommandMethod("UnsafeCmd")]
        public void UnsafeCmd()
        {
            LayoutManager layMgr = LayoutManager.Current;
            layMgr.LayoutSwitched += new LayoutEventHandler(OnLayoutSwitched);
        }

        [CommandMethod("SafeCmd")]
        public void SafeCmd()
        {
            LayoutManager layMgr = LayoutManager.Current;
            layMgr.LayoutSwitched -= new LayoutEventHandler(OnLayoutSwitched);
            layMgr.LayoutSwitched += new LayoutEventHandler(OnLayoutSwitched);
        }

        void OnLayoutSwitched(object sender, LayoutEventArgs e)
        {
            Application.ShowAlertDialog("Layout switched");
        }
    }
}

Offline frits432

  • Newbie
  • *
  • Posts: 15
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Query for EventHandlers
« Reply #4 on: November 21, 2011, 10:37:18 PM »

Quote
So it' possible to systematically remove the handler before adding it.
But this may take a while if you need to guess how often you have to remove handlers.  :reprobateur:
I'll stick to the
Code: [Select]
if (!eventsEnabled)Thanks Gile.
Frits