Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8545397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:46:56+00:00 2026-06-11T12:46:56+00:00

Would I be a good design to use Tasks for worker threads in a

  • 0

Would I be a good design to use Tasks for worker threads in a Windows Service, or should we better stick with Thread.Start()? Maybe not even start the workers as LongRunning if they are mostly idle, get triggered by FileSystemWatcher Events an do their processing using Take() off BlockingCollections.

Public Class FileWatcherService
Private _watchPaths As New List(Of String) From {"x:\Dir1","x:\Dir2","y:\Dir1", ...}
Private _workers As New List(Of Task)
Private _cancellationTokenSource As New CancellationTokenSource()
Private _cancellationToken As CancellationToken = _cancellationTokenSource.Token

Protected Overrides Sub OnStart(ByVal args() As String)
    For Each path In _watchPaths
        _workers.Add(
            Task.Factory.StartNew(
                Sub()
                    Dim fileProcessor As New FileProcessor
                    fileProcessor.StartWorking(path, _cancellationToken)
                End Sub, TaskCreationOptions.LongRunning, _cancellationToken))
    Next
End Sub

Protected Overrides Sub OnStop()
    _cancellationTokenSource.Cancel()
    Task.WaitAll(_workers.ToArray)
End Sub
End Class

Class FileProcessor
Private _newFiles As New BlockingCollection(Of String)
Sub _fileWatcher_Created(sender As Object, e As FileSystemEventArgs)
    _newFiles.Add(e.FullPath, _cancellationToken)
End Sub

Async Function ProcessNewFiles() As Task
    Do
        Await ProcessFile(_newFiles.Take(_cancellationToken))
    Loop
End Function
End Class

EDIT

The approach above does not release the worker threads when idle, because the block on Take().
The following solution uses an ActionBlock instead of the BlockingCollection. This solution does not consume threads while idly watching for new files. I spins up threads to process new files and releases them when done. And I no longer start the top-level worker tasks with LongRunning.

Class FileProcessor
Private _newFilesActionBlock As New ActionBlock(Of String)(
    Async Function(filePath)
        Await ProcessFile(filePath)
    End Function,
        New ExecutionDataflowBlockOptions With {
            .CancellationToken = _cancellationToken})

Sub _fileWatcher_Created(sender As Object, e As FileSystemEventArgs)
                        Handles __fileWatcher.Created
    _newFilesActionBlock.Post(e.FullPath)
End Sub
'...

End Class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:46:57+00:00Added an answer on June 11, 2026 at 12:46 pm

    TPL is more than welcome addition to the .NET framework. It makes your threading code much easier to work with and more readable. It allows you to make your windows service (or any other threading code) multi-threaded without having to instantiate and deal with thread pools and individual threads.

    I am using TPL in my windows service and it works great for me and I would most certainly recommend using TPL instead of classic thread pools in most cases.

    That being said there are some very rare cases where you would still want to handle the thread pools yourself, but based on your code snippet it appears you don’t really need to bother with that…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be a good way to writein C# a .NET3.5 Thread Safe DataSource
Would using MVC to write a serious game be good design and a smart
I'm not sure what would be good OO design in the following case. First
Is this a good design for a background thread that needs to be run
What would be a good design for using Solr to search in complex JSON?
I've decided that it would be good for me to move outside of my
I have some useful wpf buttons to test some functionality. It would be good
Two good examples would be google and facebook . I have lately pondered the
What would be a good way to determine if a string contains an IPv4
What would be a good approach in general to cache a web page where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.