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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:26:45+00:00 2026-05-13T00:26:45+00:00

I’m new to threading basics. I have a queue of operations to be performed

  • 0

I’m new to threading basics.

I have a queue of operations to be performed on a XML files(node add,node delete etc)

1]There are ‘n’ xml files and for each file a thread from thread pool is allocated using
ThreadPool.QueueUserWorkItem to do those file operations.

I want to achieve both concurrency and ordering of operation(important) using threads.
eg: Suppose if operations [a1,a2,a3,a4,a5] are to be performed on file “A.xml”
and operations [b1,b2,b3,b4,b5,b6,b7] are to be performed on file “B.xml” …..
I want to allocated threads such that i can perform these operations in the
same order and also concurrently(since files are different).

2]Also is it possible to assign each operation a thread and achieve concurency and preserve order.

In STA model i did something similar..

while(queue.count>0){
  File f = queue.Dequeue(); //get File from queue       
  OperationList oprlst = getOperationsForFile(f); 
// will get list-> [a1,a2,a3,a4,a5]   
  for each Operation oprn in oprlst 
  {
    performOperation(f,oprn)
    //in MTA i want to wait till operation "a1" completes and then operation "a2" will
   //start.making threads wait till file is in use or operation a(i) is in use.
  }    
}

i want to do this concurrently with operation order preservation.
Threads(of operation) can wait on one file…but
Different operations take different execution times.

i tried AutoResetEvent and WaitHandle.WaitAll(..) but it made the while loop
stop untill all ‘a(i)’ operations finish..i want both a(i) and b(j) perform concurrently.
(but ordering in a(i) and b(j))

Currently using .net 2.0 .

This is quite similar and is part of this question asked Question

  • 1 1 Answer
  • 2 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-05-13T00:26:45+00:00Added an answer on May 13, 2026 at 12:26 am

    You should avoid using thread blocking techniques like Monitor locks and WaitHandle structures in ThreadPool threads, since those threads are used by other processes. You need to have your threading be based around individual files. If an individual file doesn’t take that long to process (and you don’t have too many files), then the ThreadPool will work.

    ThreadPool Implementation

    You could just use EnqueueUserWorkItem on a file-centric method…something like this:

    private void ProcessFile(Object data)
    { 
        File f = (File)data;
    
        foreach(Operation oprn in getOperationsForFile(f))
        {
            performOperation(f, oprn);
        }
    }
    

    Then in your code that processes the files, do this:

    while(queue.Count > 0)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), queue.Dequeue());
    }
    

    If you need your calling thread to block until they are all complete, then a WaitHandle is OK (since you’re blocking your own thread, not the ThreadPool thread). You will, however, have to create a small payload class to pass it to the thread:

    private class Payload
    {
        public File File;
        public AutoResetEvent Handle;
    }
    
    private void ProcessFile(Object data)
    { 
        Payload p = (Payload)data;
    
        foreach(Operation oprn in getOperationsForFile(p.File))
        {
            performOperation(f, oprn);
        }
    
        p.Handle.Set();
    }
    
    ...
    
    WaitHandle[] handles = new WaitHandle[queue.Count];
    int index = 0;
    
    while(queue.Count > 0)
    {        
        handles[index] = new AutoResetEvent();
    
        Payload p = new Payload();
    
        p.File = queue.Dequeue();
        p.Handle = handles[index];
    
        ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessFile), p);
    
        index++;
    }
    
    WaitHandle.WaitAll(handles);
    

    Thread Implementation

    If, however, you have a large number of files (or it may take a significant amount of time for your files to process), then creating your own threads is a better idea. This also allows you to get away with omitting the WaitHandles.

    private void ProcessFile(File f)
    {     
        foreach(Operation oprn in getOperationsForFile(f))
        {
            performOperation(f, oprn);
        }
    
        p.Handle.Set();
    }
    
    private object queueLock = new object();
    
    private void ThreadProc()
    {
        bool okToContinue = true;
    
        while(okToContinue)
        {
            File f = null;
    
            lock(queueLock)
            {
                if(queue.Count > 0) 
                {
                    f = queue.Dequeue();
                }
                else
                {
                    f = null;
                }
            }
    
            if(f != null)
            {
                ProcessFile(f);
            }
            else
            {
                okToContinue = false;
            }
        }
    }
    
    ...
    
    Thread[] threads = new Thread[20]; // arbitrary number, choose the size that works
    
    for(int i = 0; i < threads.Length; i++)
    {
        threads[i] = new Thread(new ThreadStart(ThreadProc));
    
        thread[i].Start();
    }
    
    //if you need to wait for them to complete, then use the following loop:
    for(int i = 0; i < threads.Length; i++)
    {
        threads[i].Join();
    }
    

    The preceding example is a very rudimentary thread pool, but it should illustrate what needs to be done.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.