Exception while unsubscribing an event

Exception while unsubscribing an event

tamas.deri
Advocate Advocate
923 Views
4 Replies
Message 1 of 5

Exception while unsubscribing an event

tamas.deri
Advocate
Advocate

I have a modeless window, which needs to watch the DocumentChanged event. There is no problem subscribing this event, but once I call in the Closed event of the window

 

doc.Application.DocumentChanged -= EventHandler[DocumentChangedEventArgs](doc_changed)

 

it raises the following Exception:

StandardError: Exception has been thrown by the target of an invocation. I haven't found any information about this yet nor I've experienced similar in the past. It might be important that this is a python script in the pyRevit environment.
0 Likes
Accepted solutions (1)
924 Views
4 Replies
Replies (4)
Message 2 of 5

tamas.deri
Advocate
Advocate
Accepted solution

As always, I just  found the solution just after posting my question that was preceded by hours of desperate experiments. Since I'm have a modeless form, I'm not in validAPI context after it's initalization, so I have to put my unsubscribe call in an ExternalEvent...

Message 3 of 5

SONA-ARCHITECTURE
Advocate
Advocate

Hi,
I've got the same problem, on closing my Modeless form, I can't unsubscribing the ViewActivated event.

My IExternalCommand, on Execute, call the Application ShowForm in wich void I create an ExternalEvent.

Here is my class :

 

public class EE_UpdateForm : IExternalEventHandler
    {
        public bool EventRegistered { get; set; }

        public void Execute(UIApplication uiapp)
        {
            //TaskDialog.Show("ViewActivatedMethod_Execute", EventRegistered.ToString());
            if (EventRegistered)
            {
                EventRegistered = false;
                uiapp.ViewActivated -= ViewActivatedMethod;
            }
            else
            {
                EventRegistered = true;
                uiapp.ViewActivated += ViewActivatedMethod;
            }
      
        }

        private void ViewActivatedMethod(object sender, ViewActivatedEventArgs args)
        {
            TaskDialog.Show("ViewActivatedMethod", "ViewActivatedMethod");
        }

        public string GetName()
        {
            return "External Event EE_UpdateForm";
        }
    }

 

 

I passed ExternalEvent Created to My modeless form in order to Raise external Event to subscribe to the ViewActivated event.

It works fine on load, but, when I close the Form, when I do an other Raise() to unsubscibe my Event, it does not work

It works on a button click event before closing, I don't no how to do??

I saw a subject here, but can't find the solution...

 

Thanx

 

Pierre NAVARRA
SONA-Architecture.
http://www.sona-architecture.com
https://fr.linkedin.com/in/pierre-navarra-62032a107
0 Likes
Message 4 of 5

tamas.deri
Advocate
Advocate

Are you using WinForm or WPF? It is working for me with WPF, but haven't tried WinForm.

Since I'm using python for this I have a custom WPF window class inheriting the default WPF Window, and in it's constructor I assigned a custom method to it's Closed event, that calls my unsubscribing ExternalEvent

class Gui(forms.WPFWindow):
    def __init__(self, xaml_file_name, context):
        forms.WPFWindow.__init__(self, xaml_file_name, handle_esc=False)
        ...
        self.Closed += self.form_closed
        ...

    def form_closed(self, sender, args):
        ...
        unsubscribe_event.Raise()
        ...

 

0 Likes
Message 5 of 5

ShambhaveeP2ZMB
Explorer
Explorer

Hi @tamas.deri 
I'm trying to open a cloud model from its revit link instance into master model and make some changes in the linked model, the script works fine, it open up the model, makes the required changes and syncs to central as well. The only problem I'm facing is while opening the model I get this pop-up (below) and it's still there even after the script has finished running. Do you think there is a way to suppress this popup through Event Handling in Dynamo-Python?

 

ShambhaveeP2ZMB_0-1675260394712.png

 

and here is the snippet of the code I'm using to open linked documents 

 

ShambhaveeP2ZMB_1-1675260507445.png

 

further refrence, I'm facing the same issue as described in this link
https://forum.dynamobim.com/t/open-files-from-bim360-in-background/42990/11?u=shambhavee

0 Likes