C
C#8mo 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
Jester8mo 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
JakenVeina8mo 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
Jester8mo ago
i wish we had destructors like in c++ for things like this
JakenVeina
JakenVeina8mo 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
Jester8mo ago
wdym "we do"
JakenVeina
JakenVeina8mo ago
C# has finalizers
Jester
Jester8mo ago
that behavior is not that simular instead of using a disposable we could just have distructors <a:sob_extreme:1155141830757847121>
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo 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 User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo 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 User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo 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 User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo ago
whats the best way to source generate between code you wrote instead of in a seperate file?
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo ago
that sucks
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Jester
Jester8mo 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
More Posts
✅ Why do my ASP.NET Core Requests not run in parallel?```csharp var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/❔ Help with Duende Login for part of pageI am having an issue that I have been struggeling with for quite some time now. I want only part of ❔ Validation Error auto resize parentHow to auto resize parent when has validation error? So that the error text doesn't crash into other❔ ✅ Coding Test Website in ASP.NETSo im making a website that should be able to login users (from school server) and Admin/Reader shou❔ Learning platformI'm currently learning C# at CS education. It's going great, but as we're dipping our toes into abst❔ guidance on protecting sensitive details (licence details) neededQ: does anyone have any guidance on protecting sensitive details (licence name and serial) from bein❔ Convert .docx to .pdf without restrictionsI need to make a .pdf from a .docx, but there is a problem I have been looking at libraries and they❔ JWT + Microsoft.AspNetCore.Authentication.JwtBearerI added to my small (Asp.net Webapi) project JSON Web Token from tutorial but I don't uderstand thi❔ Question about Azure AD B2C and Front-End Authentication via APIHey everyone! I've got a question about Azure AD B2C. After someone signs up using the "RegisterPort❔ ✅ Making sense of dijkstra's algorithm for grid based player movement and pathfinding.I need some assistance with a grid based movement system for the game I'm working on. If anyone's in