UWP How to cancel a file search

Arsen Kovalchuk 0 Reputation points
2024-09-10T15:14:33.9233333+00:00

I have a code to search for files in a user's selected folder, the file search takes some time and I need to be able to cancel it if the user changes the folder

QueryOptions options = new QueryOptions(CommonFileQuery.OrderByName, filesExtensions) { FolderDepth = FolderDepth.Deep };
   StorageFileQueryResult queryresult = folder.CreateFileQueryWithOptions(options);
   var files = await queryresult.GetFilesAsync();

I tried to use the CancellationToken token for this, but it doesn't work properly.

 var files = await queryResult.GetFilesAsync().AsTask(cancellationTokenSource.Token);
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.