Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freeze when trying to check a file exists #46

Open
soroshsabz opened this issue Jul 21, 2016 · 16 comments
Open

Freeze when trying to check a file exists #46

soroshsabz opened this issue Jul 21, 2016 · 16 comments

Comments

@soroshsabz
Copy link

soroshsabz commented Jul 21, 2016

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:

  • Visual studio 2015 update 3
  • Xamarin forms
  • UWP app

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

@bgroenks96
Copy link

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
Copy link
Contributor

I am also experiencing this bug

@tmm360
Copy link

tmm360 commented Nov 13, 2016

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
Copy link

tmm360 commented Nov 13, 2016

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
Copy link
Author

soroshsabz commented Nov 13, 2016

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
Copy link

tmm360 commented Nov 14, 2016

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

@soroshsabz
Copy link
Author

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
Copy link

tmm360 commented Nov 14, 2016

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
Copy link
Author

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

@thumbmunkeys
Copy link

thumbmunkeys commented Dec 19, 2016

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
Copy link
Contributor

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
Copy link

nuget update?

@mentat9
Copy link

mentat9 commented Jan 16, 2018

+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
Copy link
Contributor

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

@Tamachan87
Copy link

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

@JKennedy24
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants