9

I have a windows service running that deletes folders from network drive. I want to make the deleting asynchronous. How can this be done?

Right now i am looping through the directories and calling

Directory.Delete(fullPath, true);

Thanks

stackoverflowuser
  • 22,212
  • 29
  • 67
  • 92
  • Very hard to see why that would be a requirement in a service, they already use threads. You could use another. – Hans Passant Jan 20 '12 at 22:04
  • Why should a Windows service do things asynchronously? It runs in the background anyway. Asynchronous operations are usually used in order to keep the UI responsive instead of freezing it during long running operations. – Olivier Jacot-Descombes Jan 20 '12 at 22:06
  • 1
    @OlivierJacot-Descombes: A windows service should be responsive to start/stop requests. – Adam Robinson Jan 20 '12 at 22:11
  • Have a lookie at: http://stackoverflow.com/questions/1018610/simplest-way-to-do-a-fire-and-forget-method-in-c – Oskar Kjellin Jan 20 '12 at 22:12
  • @OskarKjellin: The TPL (rather than using the `ThreadPool` class) is a more idiomatic approach in .NET 4.0 and above. – Adam Robinson Jan 20 '12 at 22:13
  • @AdamRobinson I don't think my comment is incorrect, it is likely that 4.0 is not targeted. – Oskar Kjellin Jan 20 '12 at 22:15
  • @OskarKjellin: Have a look at the tags on the question. In any case, answers do not belong in the comments. – Adam Robinson Jan 20 '12 at 22:19
  • @AdamRobinson I never said it was an answer. As stated in many other questions, links to questions are not answers. Did not notice the 4.0 tag though – Oskar Kjellin Jan 20 '12 at 22:20
  • @AdamRobinson: start/stop request responsiveness is a good point. A `Application.DoEvents();` after each loop or should be sufficient. – Olivier Jacot-Descombes Jan 20 '12 at 22:33
  • 1
    @OlivierJacot-Descombes: `Application.DoEvents` is for WinForms applications. Windows services do not have a message loop. – Adam Robinson Jan 20 '12 at 22:37
  • Put your directory delete in a method, and call it asynchronously. Very simple. Here is a great tutorial. You can also use asynchronous callbacks/events to post progress updates; http://www.codeproject.com/Articles/14931/Asynchronous-Method-Invocation – AnthonyBlake Jan 20 '12 at 22:05

2 Answers2

11

I would use the Task Parallel Library:

Task.Factory.StartNew(path => Directory.Delete((string)path, true), fullPath);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 4
    +1; beat me to it. And kudos for using the lighter-weight approach of passing the parameter instead of closing over the local. – Adam Robinson Jan 20 '12 at 22:10
  • @vcsjones : will this be like call the method asynchronously and then it will be picked by io thread later ? – stackoverflowuser Jan 20 '12 at 22:15
  • 2
    @stackoverflowuser This will call `Directory.Delete` in a task; which (may - probably) will be a thread from Thread Pool. This will allow your code to continue while the delete is happening on another thread. – vcsjones Jan 20 '12 at 22:16
  • 1
    @vcsjones Isn't this against what is mentioned here: http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx ? I surely dont grok the async subject much I think. – nawfal Mar 24 '16 at 07:07
  • The `Task.Factory.StartNew` method [should not be used](https://blog.stephencleary.com/2013/08/startnew-is-dangerous.html) without configuring the `TaskScheduler` parameter explicitly. [Even better](https://devblogs.microsoft.com/pfxteam/task-run-vs-task-factory-startnew/) don't use the `Task.Factory.StartNew`, and use `Task.Run` instead. – Theodor Zoulias Feb 23 '21 at 05:28
0

If you are looping, you could use a parallel foreach

// assuming that you have a list string paths.  
// also assuming that it does not matter what order in which you delete them
Parallel.ForEach(theListOfDirectories, x => Directory.Delete(x));
Community
  • 1
  • 1
JMarsch
  • 21,484
  • 15
  • 77
  • 125
  • FYI using the `Parallel.ForEach` method without specifying the [`MaxDegreeOfParallelism`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.paralleloptions.maxdegreeofparallelism) leads to [thread-pool starvation](https://stackoverflow.com/questions/66261010/multiple-parallel-foreach-loops-in-net/66263583#66263583). It is preferable to configure this option in order to get a consistent behavior, and not let the degree of parallelism be determined by how many threads happen to be currently available in the `ThreadPool`. – Theodor Zoulias Feb 23 '21 at 05:35