Pause an Event

Pause an Event

aclarke
Advocate Advocate
515 Views
5 Replies
Message 1 of 6

Pause an Event

aclarke
Advocate
Advocate

Hi all,

I am curious to know why my event keeps firing?

 

In my command I show how I am registering and unregistering.

 

My event keeps firing after the unregister, If I close the document the event stops.  But I want to stop the event with a command in my current open document.

 

Thanks

 

 

 

 

// ----------------------------------------------------------------------------------------
// | Test Function Command |
// ----------------------------------------------------------------------------------------
[Transaction(TransactionMode.Manual)]
internal class Cmd_RefreshFreeSize : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;
        Application app = commandData.Application.Application;
        Document doc = commandData.Application.ActiveUIDocument.Document;

        // To Register Event
        commandData.Application.ViewActivated += new EventHandler<Autodesk.Revit.UI.Events.ViewActivatedEventArgs>(Assembly.OnViewActivated);

        // To UnRegister Event
        commandData.Application.ViewActivated -= Assembly.OnViewActivated;


        return Result.Succeeded;
    }
}

// ----------------------------------------------------------------------------------------
// | Function |
// ----------------------------------------------------------------------------------------
//

internal static class Assembly
{
    internal static void OnViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
    {
        UIApplication uiapp = sender as UIApplication;
        View view = uiapp.ActiveUIDocument.ActiveView;

        if (view.IsAssemblyView)
        {
            Assembly.MessageBox(view.IsAssemblyView.ToString());
        }
        else
        {

                MessageBox("not an assembly");
        }

    }

    // ----------------------------------------------------------------------------------------
    // | Task Dialog | 
    // ----------------------------------------------------------------------------------------
    //

    internal static TaskDialogResult MessageBox(string str)
    {
        return TaskDialog.Show("Task", str);

    }

    // ----------------------------------------------------------------------------------------

    internal static TaskDialogResult MessageBox(object obj)
    {
        return TaskDialog.Show("Task", $"{obj}");

    }

    // ----------------------------------------------------------------------------------------

}

 

 

 

 

0 Likes
Accepted solutions (1)
516 Views
5 Replies
Replies (5)
Message 2 of 6

aclarke
Advocate
Advocate

I am testing my code with the AddIn Manager, it has something to do with how the AddIn Manager works.  Seems each run is independent of the last run.

 

I hope to find more explanation.

 

Thanks

 

0 Likes
Message 3 of 6

ricaun
Advisor
Advisor

Hello,

 

My first extinct was related to the new EventHandler, try to use the same event pattern.

 

// To Register Event
commandData.Application.ViewActivated += Assembly.OnViewActivated;

// To UnRegister Event
commandData.Application.ViewActivated -= Assembly.OnViewActivated;

 

I don't know what version of the AddInManager you are using, but I know an old version that I used has some problems with static code.

 

That's why I created my own AddInManager to fix this static problem. By the way, I test your original code, and works fine, the event was unregistered like should be.

 

The plugin is called AppLoader.

 

 

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Message 4 of 6

aclarke
Advocate
Advocate

Thanks for the feed back

I checked out your video(s) and website, you're doing some pretty cool stuff man!!

 

Message 5 of 6

aclarke
Advocate
Advocate

here's something strange.  If I load the code this way thru add-in manager

 

First load works fine. 

Second load does not unregister the first load, so it fires twice, which is what my OP is about. 

But the strange part is not only does it fire twice on view change, it also jumps back to the original view I was first on and fires twice then jumps back to the selected view and fires twice once again???

 

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIApplication uiapp = commandData.Application;
    Application app = commandData.Application.Application;
    Document doc = commandData.Application.ActiveUIDocument.Document;

        // To Register Event
        commandData.Application.ViewActivated -= Assembly.OnViewActivated;
        commandData.Application.ViewActivated += Assembly.OnViewActivated;

        return Result.Succeeded;
}
0 Likes
Message 6 of 6

ricaun
Advisor
Advisor
Accepted solution

AddinManager works like this: Every time you execute a command the AddinManager gonna creates a new copy of that dll/assembly and gonna executes that command.


If you have 2 commands one to add the event and the other to remove the event, and if AddinManager creates a new assembly for each time it executes a command, the remove event command gonna try to remove an event that is not registered yet.

 

Usually is a bad idea add/remove events using IExtenalCommand, and if I do, I always remove the event in the IExternalApplication shutdown. This makes sure the event gonna be removed properly.

 

If you do not remove the event properly, you gonna have an event stuck until you close your Revit.

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils