UWP - Read a file and be able to cancel the playback/opening.

Anthony.Ryck 1 Reputation point
2020-05-07T16:23:28.237+00:00

Hello,

Note : I use a translation app. Sorry if it's not always very understandable.

I'm developing a UWP application, and I'm having a problem with managing a file type, the CBZ extension.
Some files open without a problem, others the file never opens and blocks the Task.

Here's the code I use :

Task loadEbookTask = Task.Factory.StartNew(() =>
{
    Stream streamEbook = WindowsRuntimeStorageExtensions.OpenStreamForReadAsync(ebookFile).Result;

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    Content = new ZipArchive(streamEbook, ZipArchiveMode.Read, false);

        // Pour charque archive, prendre que des extensions valident.
        foreach (var file in Content.Entries)
    {
        string extension = Path.GetExtension(file.Name).ToLower();
        bool isFileExtensionOk = EbooksManager.AvailableExtensionsImage.Contains(extension);

        if (isFileExtensionOk)
        {
            ArchivesExploitable.Add(file);
        }
    }

    TotalPage = Convert.ToUInt32(ArchivesExploitable.Count());
});

if (loadEbookTask.Wait(4000))
{
    EbookCbz.LoadEbook = EbookLoad.Ok;
}
else
{
    EbookCbz.LoadEbook = EbookLoad.Timeout;
}

It's looping on :

Stream streamEbook = WindowsRuntimeStorageExtensions.OpenStreamForReadAsync(ebookFile).Result;

In Visual Studio, memory doesn't go up any more, but the Garbage Collector keeps being called.
With the Task.Wait(4000), it does not stop the Task, so it does not stop turning in background.
And if I open another file, a new task is created, and will turn into a background task.

My question is:

  • Is there a method that open a file, and that it is possible to cancel if it exceeds a certain time.
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-05-08T01:48:38.67+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You are using TaskFactory, then you can choose to abort the task after timeout.

    CancellationToken is required to abort the task, you can write like this:

    CancellationTokenSource source = new CancellationTokenSource();  
    CancellationToken token = source.Token;  
    TaskFactory factory = new TaskFactory(token);  
    var task = factory.StartNew(() =>  
    {  
        // read file  
    }, token);  
    try  
    {  
        if (task.Wait(4000))  
        {  
            EbookCbz.LoadEbook = EbookLoad.Ok;  
        }  
        else  
        {  
            EbookCbz.LoadEbook = EbookLoad.Timeout;  
            source.Cancel();  
        }  
    }  
    catch (Exception ex)  
    {  
          
    }  
    finally{  
        source.Dispose();  
        // do something...  
    }  
    

    The current running task can be aborted through the call of source.Cancel().

    You can refer to this document to learn more about the usage of CancellationToken.

    Thanks.

    1 person found this answer helpful.

  2. Anthony.Ryck 1 Reputation point
    2020-05-12T09:01:27.75+00:00

    Its my solution

    It is this method that is problematic.

    Stream streamEbook = WindowsRuntimeStorageExtensions.OpenStreamForReadAsync(ebookFsile).Result;
    

    I change my code to :

    byte[] buffer = await ebookFile.ReadBytesAsync(); 
    Stream stream = new MemoryStream(buffer);
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 
    Content = new ZipArchive(stream, ZipArchiveMode.Read, false);
    

    It's fast and if the file is corrupted, there's an exception. It's no longer in memory. Thanks for your help, I learned a new concept.