# Ask : Single window & catch OnClose event

**URL:** <https://www.ruby-forum.com/t/ask-single-window-catch-onclose-event/170650>\
**Category:** wxRuby\
**Created:** [June 20, 2009, 6:16am UTC](https://www.ruby-forum.com/t/ask-single-window-catch-onclose-event/170650 "2009-06-20T06:16:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [June 20, 2009, 6:16am UTC](https://www.ruby-forum.com/t/ask-single-window-catch-onclose-event/170650/1 "2009-06-20T06:16:05Z")

</div>

Dear all  
I have 2 question here

I’m creating desktop application using wxruby and encounter some problem  
I need my application to be able to open only one for each frame  
is there any way to do this?  
I tried to include singleton but then my frame lost its contact with its  
parent  
(my frame class is inherited from WxFrame and I use initialize method to  
assign parent)

also, I need to catch OnClose event  
how can I do that? I need to do something (delete temporary file) before  
the  
frame closed  
code example will be appreciated 🙂

Thank you

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [June 20, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/ask-single-window-catch-onclose-event/170650/2 "2009-06-20T13:56:04Z")

</div>

Hi

hendra kusuma wrote:

> I’m creating desktop application using wxruby and encounter some problem  
> I need my application to be able to open only one for each frame  
> is there any way to do this?  
> I tried to include singleton but then my frame lost its contact with  
> its parent  
> (my frame class is inherited from WxFrame and I use initialize method  
> to assign parent)  
> I’m not sure what you mean here - do you mean that only a single  
> instance of the application should be running at one time? If so, the  
> way would probably be to create some kind of lockfile on application  
> start: if the lockfile already exists, then the application is already  
> running.

If you want to have only one frame per file document, just keep a hash  
as an instance variable of the Wx::App object, with the keys being the  
file paths, and the values being the Wx::Frame objects. Then you can  
check this hash to see if a Frame is already opened for a given file.

> also, I need to catch OnClose event  
> how can I do that? I need to do something (delete temporary file)  
> before the frame closed  
> code example will be appreciated 🙂

Use evt\_close, something like:  
class MyFrame \< Wx::Frame  
def initialize  
super  
evt\_close do | e |  
delete\_temp\_file …  
e.skip  
end

note that calling ‘skip’ is important here, to allow normal processing  
to continue and the window to actually close

hth  
alex

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [June 22, 2009, 5:09am UTC](https://www.ruby-forum.com/t/ask-single-window-catch-onclose-event/170650/3 "2009-06-22T05:09:47Z")

</div>

On Sat, Jun 20, 2009 at 6:54 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > assign parent)
> 
> evt\_close do | e |  
> delete\_temp\_file …  
> e.skip  
> end
> 
> note that calling ‘skip’ is important here, to allow normal processing to  
> continue and the window to actually close

Thank you, it’s very helpful  
I solve the first problem by intercepting close event and call hide()  
instead  
therefore I don’t need to create an instance when I call the frame  
since the frame is only hide when it’s closed, I only need to show() it  
again

anyway, there is some good ideas you show me up there about making  
single  
instance and so on  
thanks, perhaps I would end up using one of those
