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 8660037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:09:20+00:00 2026-06-12T16:09:20+00:00

using C# on the .Net 4.0 framework, I have a Windows Forms main thread

  • 0

using C# on the .Net 4.0 framework, I have a Windows Forms main thread (the only one, until now) that waits for filesystem events and then must start some predefined processing on the files provided by those events.

I am planning to do the following:

  • A1. To immediately create a separate thread when the main process start;
  • A2. Have the main thread to put in a Queue (FIFO) the file names to be
    processed;
  • A3. Have the new thread triggered by a timer every n
    seconds;
  • A4. Have the new thread read the queue, if there is an item
    to perform the processing, then have it cancel the queue item just
    processed.

Because I never have programmed threads before (I am basically using the Albahari as my compass) but I definitely want to, I have a few questions just to spot possible heavy headache in advance:

  • Q1. May I incur into concurrence problems on the Queue if the main process writes only and the new ones cancel only queue items? In other words: Is synchronization a significant issue in this case?
  • Q2. I have seen that I could create a new thread from scratch or can reuse one of the threads made available from an existing pool. It is safer / simpler to use threads from the pool in this context?
  • Q3. Are there any drawbacks to keeping alive the new thread indefinitely and responding only to timer until the main process is closed?
  • 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-12T16:09:21+00:00Added an answer on June 12, 2026 at 4:09 pm

    If you are targeting .Net Framework 4, the Blocking Collection sounds like it will solve your issues; i.e. creating a new thread pooled thread when “work” items become available on the queue (added to the queue on the event handler when new files are added) and process them asynchronously on that thread.

    You can use one in a producer/consumer queue:

    E.g.:

    /// <summary>
    /// Producer/consumer queue. Used when a task needs executing, it’s enqueued to ensure order, 
    /// allowing the caller to get on with other things. The number of consumers can be defined, 
    /// each running on a thread pool task thread. 
    /// Adapted from: http://www.albahari.com/threading/part5.aspx#_BlockingCollectionT
    /// </summary>
    public class ProducerConsumerQueue : IDisposable
    {
        private BlockingCollection<Action> _taskQ = new BlockingCollection<Action>();
    
        public ProducerConsumerQueue(int workerCount)
        {
            // Create and start a separate Task for each consumer:
            for (int i = 0; i < workerCount; i++)
            {
                Task.Factory.StartNew(Consume);
            }
        }
    
        public void Dispose() 
        { 
            _taskQ.CompleteAdding(); 
        }
    
        public void EnqueueTask(Action action) 
        { 
            _taskQ.Add(action); 
        }
    
        private void Consume()
        {
            // This sequence that we’re enumerating will block when no elements
            // are available and will end when CompleteAdding is called.
            // Note: This removes AND returns items from the collection.
            foreach (Action action in _taskQ.GetConsumingEnumerable())
            {
                // Perform task.
                action();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows Forms application built using the .NET 3.5 Framework which self
I have a Windows Forms application developed using C# in .NET framework 3.5, Service
I have a program using WPF in C# that uses the .NET 4 Framework
I'm using Visual Studio 2008, .net Framework 3.5 for a Windows forms client-server app
I have a simple Windows Form application that is using the .net 3.5 compact
What I have created a very simple asp.net web service using .NET framework 3.5,
I have an app using the ADO.NET entity framework (the VS2008 version, not the
I am using the asp.net mvc with the Entity Framework. I have a list
I have asp.net application and am using Entity Framework to connect it with the
I have developed an ASP.Net (VB.Net) website which is using Entity Framework and SQL

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.