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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:06:57+00:00 2026-05-21T23:06:57+00:00

I am working on a class library that logs audit details of a web

  • 0

I am working on a class library that logs audit details of a web application in several types of datasources(file, xml, database) based on policies defined in the web configuration file.

My Audit log method has a signature similar to this:
public static void LogInfo(User user, Module module, List lst);

Web application uses this method to log important pieces of details like warnings, error and even exception details.

Since in a single workflow, there are more than 700+ calls to these methods , I thought of making them asynchronous. I used simple method from ThreadPool class called QueueUserWorkItem

ThreadPool.QueueUserWorkItem(o => LogInfo(User user, Module module, List<Object> lst) );

but this does not ensure the order in which work item was queued to it. Even though all my information was logged but entire ordering was messed up. In my text file my logs were not in the order in which they were called.

Is there a way I can control the ordering of the threads being called using QueueUserWorkItem?

  • 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-05-21T23:06:58+00:00Added an answer on May 21, 2026 at 11:06 pm

    I don’t think you can specify ordering when using QueueUserWorkItem.

    To run the logging in parallel (on some background thread), you could use ConcurrentQueue<T>. This is a thread-safe collection that can be accessed from multiple threads. You could create one work item (or a thread) that reads elements from the collection and writes them to a file. Your main application would add items to the collection. The fact that you’re adding items to the collection from a single thread should guarantee that they will be read in the right order.

    To keep things simple, you can store Action values in the queue:

    ConcurrentQueue<Action> logOperations = new ConcurrentQueue<Action>();
    
    // To add logging operation from main thread:
    logOperations.Add(() => LogInfo(user, module, lst));
    

    The background task can just take Actions from the queue and run them:

    // Start this when you create the `logOperations` collection
    ThreadPool.QueueUserWorkItem(o => {
        Action op;
        // Repeatedly take log operations & run them
        while (logOperations.TryDequeue(out op)) op();
    });
    

    If you need to stop the background processor (that writes data to the log), you can create a CancellationTokenSource and also end the while loop when the token is being cancelled (by the main thread). This cha be checked using IsCancellationRequested property (see MSDN)

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

Sidebar

Related Questions

I am working on a class library that retrieves information from a third-party web
I've got a project that I'm working Enterprise Library logging into, and that application
I'm working on a C# class library that needs to be able to read
I'm building a class library which references a file in its working directory. However,
I'm working with a C++ class-library that provides a Thread base-class where the user
I am working in a project that has two main parts: a class library
I have a WinForms application that relies on a class library DLL. Inside the
I have a class library that uses some xml files found in its own
I am working on a class library that tracks transit vehicles in close to
I am working on a class library using C#. I have designed 3 main

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.