gkngkc / UnityStandaloneFileBrowser

A native file browser for unity standalone platforms
MIT License
2.01k stars 317 forks source link

Solution Async Windows #78

Open johnmrncc1701d opened 4 years ago

johnmrncc1701d commented 4 years ago

It is possible to call OpenFilePanel in a (non-UI) thread, but you need to set the thread with TrySetApartmentState(ApartmentState.STA) and then it will work. So it is possible to have the Async version work on Windows. I thought I would share that because of how long it took me to figure it out.

awwbees commented 4 years ago

thank you! you likely just saved me a headache

coty-crg commented 3 years ago

This worked, thank you!!

iiley commented 3 years ago

Works well, thank you, is there a way for Mac?

TobiasWehrum commented 10 months ago

@johnmrncc1701d, what went wrong before you called TrySetApartmentState(ApartmentState.STA)? I'm wondering if there's some problem with my method (where I'm not doing that) that I haven't found yet.

I'm doing it like this, and so far it seems to work well on Windows 10:

public async void PressedLoadButton()
{
    var openFilePanelTask = Task.Run(() =>
    {
        return StandaloneFileBrowser.OpenFilePanel("Choose file", "", "txt", false);
    });

    await openFilePanelTask;

    var pathArray = openFilePanelTask.Result;
    // Do whatever you want with pathArray
}
johnmrncc1701d commented 10 months ago

I don't remember. It was very long ago. On Monday, August 14, 2023 at 11:12:24 AM PDT, Tobias Wehrum @.***> wrote:

@johnmrncc1701d, what went wrong before you called TrySetApartmentState(ApartmentState.STA)? I'm wondering if there's some problem with my method (where I'm not doing that) that I haven't found yet.

I'm doing it like this, and so far it seems to work well on Windows 10: public async void PressedLoadButton() { var openFilePanelTask = Task.Run(() => { return StandaloneFileBrowser.OpenFilePanel("Choose file", "", "txt", false); });

await openFilePanelTask;

var pathArray = openFilePanelTask.Result;
// Do whatever you want with pathArray

}

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

TobiasWehrum commented 10 months ago

I don't remember. It was very long ago.

It was indeed. Thanks for the quick answer anyway!