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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:47:29+00:00 2026-06-15T22:47:29+00:00

I think i have some problems with logic right now. I have used a

  • 0

I think i have some problems with logic right now.

I have used a blocking collection to make thread safe calls to other PCs. In general it looks like this:

public class MyClass
{
  private BlockingCollection<workUnit> workUnits = new BlockingCollection<workUnit>();

  public void EnQueue(workUnit item)
  {
    workUnits.Add(item);
  }

  private void DeQueue()
  {
    while (!stopFlag)
    {
      workUnit item = workUnits.Take();
      DoLongRunningDBStuff(workUnit);
    }
  }
} 

Now I want to visualize this to a user.

A user should see that

  1. items are in Queue
  2. item processing has started
  3. result of processing (mainly passed/failed/exception)

And now I got some headache.

I was thinking to do the following:

Have a Grid to display the items to users.

  1. If item is Enqueued add it to workunits and additionally to a list bound to the datagrid
  2. If item is Dequeued (consumed) update the item in the list for the grid.

What makes the headache, is how to make this thread safe, and which parts are needed to be thread safe.

If I put something which takes time behind workUnit.Add I think it could be possible, that data gets mixed.

Would something like this be feasible?

  1. If item is Enqueued add it to workunits and an additional BlockingCollection for UI
  2. If item is Dequeued, make a tryget on 2. BlockingCollection and remove it there, update the status and attach it to second list again.

Would I need an additional lock aound 1 and 2? If so, wouldn’t it block completly the add if waiting for Take?

Is there an easy solution or approach to visualize, what is going on?

  • 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-15T22:47:31+00:00Added an answer on June 15, 2026 at 10:47 pm

    I will try do it this way:

    public class MyClass
    {
      private BlockingCollection<workUnit> workUnits = new BlockingCollection<workUnit>();
    
      public void EnQueue(workUnit item)
      {
        workUnits.Add(item);
      }
    
      private void DeQueue()
      {
        while (!stopFlag)
        {
          workUnit item = workUnits.Take();
          item.SetState("Processing Started");
          try
          {
              DoLongRunningDBStuff(workUnit);
              item.SetState("Processing Successful");
          }
          catch
          {
              item.SetState("Processing Failed");
          }
        }
      }
    } 
    

    in this example I would then make workItem.SetState(...) fire an event that will update UI for the particular item. However, because the event is raised in a non-UI thread, it will be the handler of the event (the form displaying the grid I would assume) that would need to post the update into the context of the UI thread (e.g. If you are using WinForms you would call the Invoke method of the control displaying the data).

    In another (preferred) suggestion I would do the following (if you can use the TPL in .NET 4.0 and later):

    public class MyClass
    {
      public Task EnQueue(workUnit item)
      {
        // Schedule the work on the thread pool. 
        // If you need limited concurrency here, there are schedulers to enable this.
        return Task.Run(() => DoLongRunningDBStuff(item));
      }
    } 
    

    And if you use .NET 4.5 you would be able to use the await feature that would automatically synchronise the continuation of the task in the context of the UI thread. E.g. in the on the caller’s side (assuming it is initiated on the UI thread) you would simply do the following:

    private async void btnAddItem_Click(object sender, EventArgs e)
    {
        var item = new workUnit();
    
        // TODO: Add item on UI here
    
        try
        {
            await myClass.EnQueue(item);
    
            // TODO: Update UI with success result here (no context synchronisation is needed here it is already in the UI context)
        }
        catch
        {
            // TODO: Update UI with error result here (no context synchronisation is needed here it is already in the UI context)
        }
    }
    

    In both examples you do not even need any locking, you simply need to have the updates posted to the correct context (and in the last example that is not even explicitly needed, the compiler takes care of it for you)

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

Sidebar

Related Questions

I have some serious problems with my app right now. Okay, so there are
Im starting with CoCos2d development and I think I have some issues with nodespace,
I have some code which I think has extra release statements. Is the code
I have some code which I think should compile, but doesn't: import java.io.InputStream; import
I have some data that I have to display as a table. I think
I think I've configured Box2d to have some sort of maximum velocity for any
I am trying to think data in terms of sets but have some questions
Really quick here... I think I have the answer, but just looking for some
I think we may have trouble with our existing project. For some reasons we
I think some of you have seen the cartoon wars game. does anybody know

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.