How Stop or Cancel revit closing

How Stop or Cancel revit closing

Anonymous
Not applicable
2,619 Views
10 Replies
Message 1 of 11

How Stop or Cancel revit closing

Anonymous
Not applicable

HI everyone

I developing my addin using C# 2012, I need my application stop the closure revit. I have a routine that is executed when a button of RibbonPanel press and public Boolean variable is assigned true. Whether revit closing the variable = false, revit not close. I want to force the user to press the button before closing revit. That event or method I can use?. i try with DocumentClosing but this method run only if a document closing and not when revit closing .

 

thanks!

0 Likes
2,620 Views
10 Replies
Replies (10)
Message 2 of 11

arnostlobel
Alumni
Alumni
It is not possible to prevent the end-user from closing Revit, not from via the Revit API anyway. I suppose you could use the Windows System API to prevent Revit closing, by stealing windows messages, for example. However, that may lead to unexpected behavior, sometime.

Arnošt Löbel
Arnošt Löbel
0 Likes
Message 3 of 11

jeremytammik
Autodesk
Autodesk

Your add-in OnShutdown method should be called when and only when Revit is closing. That will at least give you a chance to display the message to the user and "force her to press a button", if you really think that is a good idea, even if it does not enable you to prevent Revit from closing.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 11

Moustafa_K
Collaborator
Collaborator

You can try adding an Event Handler to stop Revit from Closing  you can add that on startup

 

 

  public Result OnStartup(UIControlledApplication Application)
        {
          ///some code

                Application.ApplicationClosing += Application_ApplicationClosing;

                return Result.Succeeded;
            }

        private void Application_ApplicationClosing(object sender, Autodesk.Revit.UI.Events.ApplicationClosingEventArgs e)
        {
         if(yourvariable== false)   e.Cancel();
        }
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 5 of 11

jeremytammik
Autodesk
Autodesk

Good suggestion!

 

You would have to check fiurst whether the application closing event is cancellable:

 

ApplicationClosingEventArgs is derived from RevitAPIEventArgs, and the RevitAPIEventArgs.Cancellable property indicates whether an event may be cancelled by an event delegate.

 

Can you confirm that?

 

Thank you!

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 6 of 11

arnostlobel
Alumni
Alumni

ApplicationClosing events aren't cancellable. It is by design.

If you try calling Cancel() on an event that is not cancellable, Revit throws an exception.

 

This is basically what I mean by stating that Revit cannot be prevented from shutting down via the Revit API.

Arnošt Löbel
Message 7 of 11

jeremytammik
Autodesk
Autodesk

... as expected ...

 

Sorry Mostafa90, a shame about your good idea, but no cigar this time  🙂



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 8 of 11

Moustafa_K
Collaborator
Collaborator
Yeaah... 🙂 tried to Help any way.
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 9 of 11

Anonymous
Not applicable

Thanks guys!!!

0 Likes
Message 10 of 11

Anonymous
Not applicable

Hello, I have the same problem now. Is there a solution to this topic?

0 Likes
Message 11 of 11

jeremytammik
Autodesk
Autodesk

The Revit API will not help you with this, as explained above.

 

Therefore, you will have to treat this as a generic .NET and Windows question, on how to prevent an arbitrary application from closing. 

 

For instance, you can search for something like 'prevent windows application from closing .net':

 

https://duckduckgo.com/?q=prevent+windows+application+from+closing+.net

  

This thread suggest some valid solutions:

 

https://serverfault.com/questions/709765/prevent-users-from-closing-certain-application

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes