question

Anthony-Ryck avatar image
0 Votes"
Anthony-Ryck asked RichardZhang-MSFT commented

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

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.

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardZhang-MSFT avatar image
1 Vote"
RichardZhang-MSFT answered RichardZhang-MSFT converted comment to answer

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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the answer.
The Task is not stopped, and the file load continuously (see both images). The CPU continues to rotate, and the GC keeps turning. We can see the use of the disk and the CPU, while the 2 Tasks are already in timeout.


8036-01-cpu.jpg

0 Votes 0 ·

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.

0 Votes 0 ·

Hello, you seem to have found a solution here. You can post your own solution below instead of close the problem

0 Votes 0 ·
Anthony-Ryck avatar image
0 Votes"
Anthony-Ryck answered RichardZhang-MSFT commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, you posted your answer, that's great. I have converted it for you. You can accept your answer and change the status of the question. Thank you.

0 Votes 0 ·