dsplaisted / PCLStorage

PCL Storage - Portable Storage APIs
Microsoft Public License
305 stars 94 forks source link

Freeze when trying to check a file exists #46

Open soroshsabz opened 8 years ago

soroshsabz commented 8 years ago

ITNOA

Hi,

When I try to check exists file such as below code

public static async Task<Stream> GetStreamAsync(string path)
{
    IFolder rootFolder = FileSystem.Current.LocalStorage;
    ExistenceCheckResult isExistFile = await rootFolder.CheckExistsAsync(path);
    if (isExistFile == ExistenceCheckResult.NotFound)
        throw new FileNotFoundException($"path: {path}");
    IFile file = await rootFolder.GetFileAsync(path);
    return await file.OpenAsync(FileAccess.Read);
}

My platform:

My process freeze, I found one problem similar to me from here.

bgroenks96 commented 8 years ago

This appears to be a bug with the behavior of the method AwaitExtensions.SwitchOffMainThreadAsync.

The workaround (at least that I've found) is to spawn a new thread using ThreadPool or the Thread class to call the PCLStorage APIs. You can use a TaskCompletionSource to asynchronously await the results of the new thread's task.

JKennedy24 commented 8 years ago

I am also experiencing this bug

tmm360 commented 8 years ago

Can you fix this? I'm using a PCL library with profile44, I don't have Thread class, and importing ThreadPool requires Windows.Foundation.UniversalApiContract and Windows.Foundation.FoundationContract that gives problems to me.

tmm360 commented 8 years ago

Also running on an other thread doesn't work for me. I've tried to implement this into the UWP solution, and inject it into the PCL library where I'm using PCLStorage. It still freeze on await Task.Delay(10).

class AsyncRunner : IAsyncRunner
{
    public async Task RunOnOtherThread(Action action)
    {
        var asyncAction = ThreadPool.RunAsync(_ => action());
        while (asyncAction.Status != Windows.Foundation.AsyncStatus.Completed)
            await Task.Delay(10);
    }
}
soroshsabz commented 8 years ago

Maybe this helpful for you, try some function like below

public static async Task<ExistenceCheckResult> CheckExists(this IFolder rootFolder, string fileName)
{
    return await Task.Run(() => rootFolder.CheckExistsAsync(fileName)).ConfigureAwait(false);
}

and call it synchronous with get Result to resolve your problem temporary.

tmm360 commented 8 years ago

Thank you, it works but only sometimes. It still can freeze, on CheckExists and on other calls. We really need a fix...

soroshsabz commented 8 years ago

Yes, you right. I need to fix it, but when I call all asynchronous function with top pattern, I could not see freeze any more. Did you sure call all API with top pattern?

tmm360 commented 8 years ago

I've upgrade every single call to the library with your pattern, and it seems to work now. Before I had OpenAsync on IFile to update, and it froze on alternate moments. Thank you! Are you a maintainer?

soroshsabz commented 8 years ago

You are welcome :) No, I am not maintainer. Unfortunately, I do not know why this library is not maintained continuously.

thumbmunkeys commented 7 years ago

I think the error is here:

public async Task<ExistenceCheckResult> CheckExistsAsync(string name, CancellationToken cancellationToken) { var result = await _wrappedFolder.GetItemAsync(name).AsTaskNoThrow(cancellationToken);

ConfigureAwait(false) is missing

JKennedy24 commented 7 years ago

From the comments this seems like a 1 line change, but as the last check in was Jan 2015. I'm not holding my breath. Frustrating

iWeaverMan commented 7 years ago

nuget update?

mentat9 commented 6 years ago

+1 Getting this fix in the nuget package would be most helpful: still repros on 1.0.2. Not surprising since it was published 1/18/2015. Can someone make another build, so we can get the fixed library via nuget? Help with this would be most appreciated!

JKennedy24 commented 6 years ago

fixed this in the source ages ago. Still no nuget update!

Tamachan87 commented 5 years ago

2019, getting this problem with the Nuget package too. Can we update this package?

JKennedy24 commented 5 years ago

@Tamachan87 Ditch this nuget entirely and swap it all for File and Directory which have now been implemented for .Net standard 2.0