1

I want to make treeView of a hierarchy of files, which name match pattern and file has searched words. I need run it in task and have an ability of stopping and continuing this task or cancel it when search started with different parameters, I accomplished stoping/continuing with ManualResetEvent object. But I don't know how to cancel this task when async function is called again (Cancellation token doesn't work from async function). Have you any ideas or better way of doing it?

Code where I demonstrate how I use CancellationTokenSource to cancel last async function call.

private void buildFileSystemTreeView(TreeNode CurrentTreeNode, DirectoryInfo directoryInfo) { foreach (var f in directoryInfo.GetFiles(this.search_pattern)) { terminate.WaitOne(); if (this.tokenSource.IsCancellationRequested) { return; } CurrentTreeNode.Nodes.Add(f.Name); this.CurrentProcessingFileStatusLabel.Text = f.Name; this.ProcessedFilesCountStatusLabel.Text = $"{++processed_files_count,3} files processed"; } foreach (var d in directoryInfo.GetDirectories()) { CurrentTreeNode.Nodes.Add(d.Name); int i = CurrentTreeNode.Nodes.Count - 1; this.buildFileSystemTreeView(CurrentTreeNode.Nodes[i], new DirectoryInfo(d.FullName)); } } async void start_search() { this.tokenSource?.Cancel(); this.tokenSource = new CancellationTokenSource(); this.token = tokenSource.Token; DirectoryInfo directoryInfo = new DirectoryInfo(path); this.start_search_treeNode = new TreeNode(directoryInfo.Name); await Task.Run(() => { Thread.CurrentThread.IsBackground = true; this.buildFileSystemTreeView(this.start_search_treeNode, directoryInfo); }, token); } 
4
  • 3
    Welcome to StackOverflow! Please read How to Ask. In particular, please post the code you've tried (which didn't work) Commented Jul 21, 2020 at 10:57
  • You have a windows form which is already async so you do not need to add another async method. See msdn background worker example : learn.microsoft.com/en-us/dotnet/api/… Commented Jul 21, 2020 at 11:15
  • what is terminate ? are you sure that code doesn't block on terminate.WaitOne(); ? Commented Jul 21, 2020 at 11:21
  • @Selvin terminate is ManualResetEvent object, you could initiate it with ManualResetEvent(true), which means it start with signaled state Commented Jul 21, 2020 at 11:24

1 Answer 1

1

The buildFileSystemTreeView should take the CancellationToken as a parameter. Your current code checks the tokenSource field, and this field is replaced each time search_start is run. So it will have a very small chance of actually cancelling.

When dealing with multi-threaded code like this it helps if you never change anything that is accessed by multiple threads. With the exception of things that are explicitly threadsafe, like a cancellationToken. For example

this.CurrentProcessingFileStatusLabel.Text = f.Name;

will probably fail since UI types can only be safely accessed by the main thread. CurrentTreeNode.Nodes.Add(f.Name) is probably also unsafe.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.