# RhinoDoc.EndSaveDocument firing before save?

**URL:** <https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243>\
**Category:** Rhino Developer\
**Created:** [January 21, 2014, 2:02am UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243 "2014-01-21T02:02:05Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [January 21, 2014, 2:02am UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/1 "2014-01-21T02:02:05Z")

</div>

I’m trying to do something immediately after Rhino documents are saved. I figured that RhinoDoc.EndSaveDocument was the event to subscribe to, but it’s not working. I created a plugin with only this additional code (no commands):

```
protected override LoadReturnCode OnLoad(ref string errorMessage)
{
    Rhino.RhinoDoc.EndSaveDocument += (s, e) =>
    {
        try { System.IO.File.Copy(e.FileName, "C:/output.3dm", true); }
        catch (System.Exception ex)
        {
            Rhino.RhinoApp.WriteLine(
                "Could not copy file post-saving: {0}", 
                ex.Message);
        }
    };
    return base.OnLoad(ref errorMessage);
}

```

My expectation was that a copy of any saved Rhino document would appear as C:\output.3dm. Instead, creating a new document, adding a box or something, and then attempting to save as “C:\test.3dm” yields this output:

> Command: \_Save  
> Could not copy file post-saving: Could not find file ‘C:\test.3dm’.  
> File successfully written as C:\test.3dm

And sure enough, C:\test.3dm exists, but C:\output.3dm doesn’t. RhinoDoc.EndSaveDocument isn’t documented, but given the existence of RhinoDoc.BeginSaveDocument, I assumed it did what I wanted. However, it appears to fire before the document is saved, not after. Is there any way to do what I want?

---

<div class="post-metadata">

**Author:** ![jordy1989](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jordy1989/32/282567_2.png) [@jordy1989](https://discourse.mcneel.com/u/jordy1989)\
**Post date:** [January 21, 2014, 3:26pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/2 "2014-01-21T15:26:54Z")

</div>

Hi there,

```
Protected Overrides Function OnLoad(ByRef errorMessage As String) As Rhino.PlugIns.LoadReturnCode
    
AddHandler Rhino.RhinoDoc.EndSaveDocument, AddressOf SaveDocument
    
Return Rhino.PlugIns.LoadReturnCode.Success
    
End Function
    
Private Function SaveDocument(ByVal sender As Object, ByVal e As Rhino.DocumentSaveEventArgs)
    
 'Do what you want to do
 'For this to work first use a command thats in your plugin. Don't know how to load it on rhinoopening
 MsgBox("hi")
    
 Return Nothing
    
End Function

```

This is it for [vb.net](http://vb.net).  
If you first use one of your own command to addhanddler it will work 🙂

---

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [January 21, 2014, 5:56pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/3 "2014-01-21T17:56:46Z")

</div>

As you can see in my example code, I’m able to subscribe to EndSaveDocument, and it’s firing. The problem is that it’s firing before the document is saved, and I need to do something after the document is saved.

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [January 21, 2014, 6:23pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/4 "2014-01-21T18:23:33Z")

</div>

I guess one way around this would be to set up a file watcher in your EndSaveDocument event.

---

<div class="post-metadata">

**Author:** ![jordy1989](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jordy1989/32/282567_2.png) [@jordy1989](https://discourse.mcneel.com/u/jordy1989)\
**Post date:** [January 22, 2014, 7:51am UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/5 "2014-01-22T07:51:19Z")

</div>

I see, sorry. Missed that one 🙂 Thought this was the problem.  
Maybe have a background worker?

First when the file is opened have a variable that has current time.  
Have another variable that keeps changing and checking if the save time is changed.  
Compare these 2 and if they dont agree. Set your action to work.

System.IO.File.GetLastWriteTime(file & location)

---

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [April 4, 2014, 5:58pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/6 "2014-04-04T17:58:01Z")

</div>

I have tried to work around this with a filesystem watcher, and it’s sort of functional, but it definitely doesn’t work when the document save has been triggered by a user as a result of Rhino being closed; at some point, my filesystem watcher handler is going to be interrupted by the fact that the program has been unloaded. Or sometimes it doesn’t, and I have a ghost Rhino process hanging around, and some other cleanup code never gets run. I just don’t see any way of avoiding these unmanageable race conditions (via checking the filesystem directly) without a proper event firing after a document save. Is there any chance that this will get changed in a future RhinoCommon?

---

<div class="post-metadata">

**Author:** ![menno](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/menno/32/770384_2.png) [@menno](https://discourse.mcneel.com/u/menno)\
**Post date:** [April 5, 2014, 6:23pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/7 "2014-04-05T18:23:26Z")

</div>

Maybe the `RhinoApp.Closing` event can help you out for the case of save-on-close that you describe above. Hopefully that event gets fired _after_ the file has been saved…

I have to say though, it is not expected behaviour that `EndSaveDocument` is triggered before the file is written. With you, I hope that it gets changed in the future.

---

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [June 30, 2015, 12:20am UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/8 "2015-06-30T00:20:00Z")

</div>

This issue was recently closed on Github ([https://github.com/mcneel/rhinocommon/issues/168](https://github.com/mcneel/rhinocommon/issues/168)) as unreproducable by @dale, but it’s still affecting me (x64, SR11, Windows 8), and since he told me to start a thread here to discuss it, I’m just bumping my (super) old one. (I’ve been using the workaround @stevebaer suggested this past year.)

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [June 30, 2015, 4:18pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/9 "2015-06-30T16:18:22Z")

</div>

Hi Cody,

Try this:

1.) When you detect a EndSaveDocument event, set a flag indicating such.  
2.) When you detect an Idle event - RhinoApp.Idle - check your flag (and check for the file in this case).

Does this help?

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [June 30, 2015, 4:19pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/10 "2015-06-30T16:19:36Z")

</div>

Here is a conversation on events that is worth reading.

> **[Event Handling - Best strategies for handling multiple events](http://discourse.mcneel.com/t/event-handling-best-strategies-for-handling-multiple-events/22175)**
>
> Hello, I have a question about event handling, in particular, how to best handle multiple 'simultaneous' events when they are triggered and what strategies are recommended. An example might be this: A user performs a 'join' on a collection of...

---

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [June 30, 2015, 8:59pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/11 "2015-06-30T20:59:53Z")

</div>

Ok, using RhinoApp.Idle seems to be doing the trick, but I am still curious about RhinoDoc.EndSaveDocument’s behavior. If it’s not firing before the save is complete, then why doesn’t the file exist on disk during the handler’s execution?

---

<div class="post-metadata">

**Author:** ![stevebaer](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stevebaer/32/473567_2.png) [@stevebaer](https://discourse.mcneel.com/u/stevebaer)\
**Post date:** [June 30, 2015, 10:28pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/12 "2015-06-30T22:28:04Z")

</div>

> [@rosecodym](#):
>
> If it’s not firing before the save is complete, then why doesn’t the file exist on disk during the handler’s execution?

Rhino writes to a temp file and at the very last moment moves that temp file to the actual location.

---

<div class="post-metadata">

**Author:** ![rosecodym](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/rosecodym/32/171851_2.png) [@rosecodym](https://discourse.mcneel.com/u/rosecodym)\
**Post date:** [July 21, 2015, 2:27pm UTC](https://discourse.mcneel.com/t/rhinodoc-endsavedocument-firing-before-save/5243/13 "2015-07-21T14:27:21Z")

</div>

An update for anyone also trying to use this solution: You need to also check the flag in RhinoApp.Closing, because if a document is closed as part of Rhino closing, Idle will never fire again.
