© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
83 replies
Tim

❔ ✅ Why does this block the UI thread?

public async void FindWindowHandle(HandleWindowFind handleWindowFind)
{
    if (!started) throw new InvalidOperationException("Process not started");
    if (playerWindowHandle != IntPtr.Zero)
    {
        handleWindowFind(playerWindowHandle);
        return;
    }

    var finding = IntPtr.Zero;
    await Task.Run(() =>
    {
        while (finding == IntPtr.Zero)
        {
            Thread.Yield();
            finding = GetWindow(_hostWindow, 5);
        }
    });
    handleWindowFind(playerWindowHandle = finding);
}
public async void FindWindowHandle(HandleWindowFind handleWindowFind)
{
    if (!started) throw new InvalidOperationException("Process not started");
    if (playerWindowHandle != IntPtr.Zero)
    {
        handleWindowFind(playerWindowHandle);
        return;
    }

    var finding = IntPtr.Zero;
    await Task.Run(() =>
    {
        while (finding == IntPtr.Zero)
        {
            Thread.Yield();
            finding = GetWindow(_hostWindow, 5);
        }
    });
    handleWindowFind(playerWindowHandle = finding);
}

I have this function. Calling this seems to be blocking the thread that it's called from.
Did I do something wrong here, or should this not block and maybe something else is causing that?
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
Next page

Similar Threads

❔ Threading-unsure im doing this correctly, Ui thread gets frozen.
C#CC# / help
4y ago
Why does this work the way it does
C#CC# / help
8mo ago
why does this Not work?
C#CC# / help
3y ago
❔ Why does it do this?
C#CC# / help
3y ago