© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
42 replies
The Don

✅ Is use of IDisposable, using keyword for a UI wait indicator incorrect?

I have a wait indicator for WPF that implements
IDisposable
IDisposable
. When creating a new object of this
IDisposasble
IDisposasble
implementation wrapped in a
using
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
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()
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()
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!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

IDisposable and using keyword?
C#CC# / help
2y ago
❔ What of use is the keyword "event"
C#CC# / help
3y ago
❔ ✅ Incorrect use of Round()?
C#CC# / help
3y ago
✅ Purpose and use of nameof() keyword
C#CC# / help
12mo ago