UnityStandaloneFileBrowser icon indicating copy to clipboard operation
UnityStandaloneFileBrowser copied to clipboard

Solution Async Windows

Open johnmrncc1701d opened this issue 5 years ago • 6 comments

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.

johnmrncc1701d avatar Apr 08 '20 01:04 johnmrncc1701d

thank you! you likely just saved me a headache

awwbees avatar Apr 22 '20 15:04 awwbees

This worked, thank you!!

coty-crg avatar Sep 30 '20 18:09 coty-crg

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

iiley avatar Jan 06 '21 10:01 iiley

@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
}

TobiasWehrum avatar Aug 14 '23 18:08 TobiasWehrum

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: @.***>

johnmrncc1701d avatar Aug 14 '23 21:08 johnmrncc1701d

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

It was indeed. Thanks for the quick answer anyway!

TobiasWehrum avatar Aug 14 '23 22:08 TobiasWehrum