✅ Is use of IDisposable, using keyword for a UI wait indicator incorrect?
I have a wait indicator for WPF that implements
IDisposable
. When creating a new object of this IDisposasble
implementation wrapped in a using
keyword, it initializes the wait indicator on my WPF screen. Upon disposing, the wait indicator disappears.
This is the code I used before implementing IDisposable
:
Here I have an early return, so I have to call waiter.hide()
twice. This is a simple case, so it may seem trivial to call it more than once, but this can cause problems when the code is more complicated. Forgetting to hide()
will obviously hang the UI.
On the other hand, using this code:
Seems more simplistic and readable. I am wondering if there are any problems with this. If so, are there any alternatives to this?
Thanks!19 Replies
yeah you could do this or put it all in a
try {} finally{}
and hide in the finally which is baiscally the same thing as what ur doing with the disposable nowone might argue you this is abuse of the using/IDisposable pattern, to make an object IDisposable that doesn't actually have umanaged resources that need to be properly freed.
you're really just wanting to leverage the syntax sugar of the using keyword, for something that isn't resource management
me, I think that's a weak argument
i wish we had destructors like in c++ for things like this
although, I think it does have some merit, as a point about code clarity, I.E. make sure your code and/or documentation is quite clear about what's going on with this IDisposable object you're creating.
I mean, we do, but practically speaking, that's what IDisposable in C# is for
wdym "we do"
C# has finalizers
that behavior is not that simular
instead of
using
a disposable we could just have distructors <a:sob_extreme:1155141830757847121>Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
i dont mean freeing the managed object at the end of a scope
but now this made me think
<a:aPES_Think:493353113332219924>
destructors could probably only work for ref structs or at least not reference types.
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
a deconstructor in C++ also doesnt run for a referemce type at the end of a scope. it runs when you delete the object
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
im thinking of a way to have this behavior without having to write
using
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
whats the best way to source generate between code you wrote instead of in a seperate file?
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
that sucks
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
i want to get better at source generating, i like it a lot
i wrote one that generated C++ code from C# code