467,923 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Disposable & 3rd Party Lib & GUI

Hi,

I'm wrapping a 3rd party .NET 2.0 CF library which mandates Dispose be
called.

So, I mandate Dispose be called on my library...I follow the Disposable
pattern described on MSDN...and I Dispose of the wrapped 3rd party
library in there.

My library is meant to be used in Forms.

My question is simply -- what's the proper way to shut down a form?
There is:

~ destructor

<form>_Closing( )

<form>_Closed( )

should I call one of the following, and if so...where...->

Application.Exit( );

this.Close( );

Where should I issue the Dispose( ) on my library?

Jan 16 '07 #1
  • viewed: 1384
Share:
3 Replies

davis wrote:
Hi,

I'm wrapping a 3rd party .NET 2.0 CF library which mandates Dispose be
called.

So, I mandate Dispose be called on my library...I follow the Disposable
pattern described on MSDN...and I Dispose of the wrapped 3rd party
library in there.

My library is meant to be used in Forms.

My question is simply -- what's the proper way to shut down a form?
There is:

~ destructor

<form>_Closing( )

<form>_Closed( )

should I call one of the following, and if so...where...->

Application.Exit( );

this.Close( );

Where should I issue the Dispose( ) on my library?
I see you suggest Application.Exit in your list. Does it mean that your
form is only closed when the application shuts down? In this case, I
don't think you even need to call Dispose(). Resources will all be
freed when the application is shut down.

Jan 16 '07 #2
Where should I issue the Dispose( ) on my library?

I see you suggest Application.Exit in your list. Does it mean that your
form is only closed when the application shuts down? In this case, I
don't think you even need to call Dispose(). Resources will all be
freed when the application is shut down.
Hi Virgil, this is not the case...especially in the compact framework
(at least what I have found). Getting an application to really exit
seems a tricky endeavor especially when you are using unmanaged
resources.

My library uses System.IO.Stream as the means to communicate directly
with the underlying hardware that is exposed from the 3rd party
library.

The 3rd party library mandates Dispose be called only once and when the
application exits. If it is called twice it throws
ObjectDisposedException.

If it is not called and the process unexpectedly terminates, then the
internal state machine of the hardware gets all screwed up and may
require a hard reset of the device. This really stinks, but this is
the way it is as I have no insight into the 3rd party code. So I have
to protect for all measures where the application terminates via any
kind of exception and make sure this doesn't happen.

This is why I implemented the Disposable pattern as per the example
here http://msdn2.microsoft.com/en-us/lib...isposable.aspx

On the main UI form, there is a Window Style -MinimizeBox property.
In CF, when set to True, the app does not terminate when the X button
is pressed, but it hides it. If set to false, it changes the X to an
Ok icon...and when pressed it is supposed to terminate.

Thus, in Form_Closing( ) I call my libraries Dispose( ), which in turn
calls the 3rd Party Library Dispose( ) -- and hope it works...but when
I run the remote process viewer on the device, I see the .exe is still
running, and not sure why...which led me to ask my original question --
what is the recommended practice here?

Jan 16 '07 #3

davis wrote:
Where should I issue the Dispose( ) on my library?
I see you suggest Application.Exit in your list. Does it mean that your
form is only closed when the application shuts down? In this case, I
don't think you even need to call Dispose(). Resources will all be
freed when the application is shut down.

Hi Virgil, this is not the case...especially in the compact framework
(at least what I have found). Getting an application to really exit
seems a tricky endeavor especially when you are using unmanaged
resources.

My library uses System.IO.Stream as the means to communicate directly
with the underlying hardware that is exposed from the 3rd party
library.

The 3rd party library mandates Dispose be called only once and when the
application exits. If it is called twice it throws
ObjectDisposedException.

If it is not called and the process unexpectedly terminates, then the
internal state machine of the hardware gets all screwed up and may
require a hard reset of the device. This really stinks, but this is
the way it is as I have no insight into the 3rd party code. So I have
to protect for all measures where the application terminates via any
kind of exception and make sure this doesn't happen.

This is why I implemented the Disposable pattern as per the example
here http://msdn2.microsoft.com/en-us/lib...isposable.aspx

On the main UI form, there is a Window Style -MinimizeBox property.
In CF, when set to True, the app does not terminate when the X button
is pressed, but it hides it. If set to false, it changes the X to an
Ok icon...and when pressed it is supposed to terminate.

Thus, in Form_Closing( ) I call my libraries Dispose( ), which in turn
calls the 3rd Party Library Dispose( ) -- and hope it works...but when
I run the remote process viewer on the device, I see the .exe is still
running, and not sure why...which led me to ask my original question --
what is the recommended practice here?
Well then, I'm clueless (I'm kinda new to C# and .NET). Maybe that your
3rd party library didn't implement the finalizer of their IDisposable
correctly (because it is supposed to call Dispose(false), and if it
did, well, as I wrote in my other message, you wouldn't even have to
call Dispose()).

Maybe The problem isn't with the Dispose(), but with some invalid stuff
you perform with the hardware that messes things up? Or with the order
in which stuff is disposed in your app?

Anyway, to answer you question, personnally, I put application's
initialization code in Program.Main(), before the "Application.Run(new
MainForm());" line, an the finalization code after that line. I
wouldn't put application level finalization in Form_Closing(), it
doesn't seem proper to me. There may be cases where this isn't called
(wild guess here, I'm not sure).

Jan 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by rbt | last post: by
7 posts views Thread by header_question | last post: by
17 posts views Thread by Bruno | last post: by
9 posts views Thread by James Stroud | last post: by
reply views Thread by bboldi | last post: by
11 posts views Thread by toton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.