C
C#13mo ago
The Don

✅ 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:
async Task LongRunningTask1()
{
_waitIndicator.Show("Waiting")

await Task1();

if(cancel)
{
_waitIndicator.Hide(); // Accounts for early return

return;
}

await Task2();

_waitIndicator.Hide()
}
async Task LongRunningTask1()
{
_waitIndicator.Show("Waiting")

await Task1();

if(cancel)
{
_waitIndicator.Hide(); // Accounts for early return

return;
}

await Task2();

_waitIndicator.Hide()
}
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:
async Task LongRunningTask2()
{
using(new _waitIndicator.Show("Waiting"))
{
await Task1();

if(cancel)
{
// No need to "hide" the wait indicator, instead, it is done when the
// using statement ends.
return;
}

await Task2();
}
}
async Task LongRunningTask2()
{
using(new _waitIndicator.Show("Waiting"))
{
await Task1();

if(cancel)
{
// No need to "hide" the wait indicator, instead, it is done when the
// using statement ends.
return;
}

await Task2();
}
}
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
Jester
Jester13mo ago
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 now
JakenVeina
JakenVeina13mo ago
one 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
Jester
Jester13mo ago
i wish we had destructors like in c++ for things like this
JakenVeina
JakenVeina13mo ago
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
Jester
Jester13mo ago
wdym "we do"
JakenVeina
JakenVeina13mo ago
C# has finalizers
Jester
Jester13mo ago
that behavior is not that simular instead of using a disposable we could just have distructors <a:sob_extreme:1155141830757847121>
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
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. thinkswing
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
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
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
im thinking of a way to have this behavior without having to write using
{
using var thing = new T();
// gets disposed here
}
{
using var thing = new T();
// gets disposed here
}
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
whats the best way to source generate between code you wrote instead of in a seperate file?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
that sucks
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester13mo ago
i want to get better at source generating, i like it a lot i wrote one that generated C++ code from C# code
Want results from more Discord servers?
Add your server