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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:08:54+00:00 2026-05-12T22:08:54+00:00

Ok, playing around with the .Net 4.0 Parellel Extensions in System.Threading.Tasks. I’m finding what

  • 0

Ok, playing around with the .Net 4.0 Parellel Extensions in System.Threading.Tasks. I’m finding what seems like weird behaivor, but I assume I’m jsut doing something wrong. I have an interface and a couple implementing clases, they’re simple for this.

interface IParallelPipe
{
    void Process(ref BlockingCollection<Stream> stream, long stageId);
}

class A:IParallelPipe
{
    public void Process(ref BlockingCollection<Stream> stream, long stageId)
    {
        //do stuff
    }
}

class B:IParallelPipe
{
    public void Process(ref BlockingCollection<Stream> stream, long stageId)
    {
        //do stuff
    }
}

I then have my class that starts things off on these. This is where the problem arises. I essentially get information about what implementing class to invoke from a type passed in and then call a factory to instantiate it and then I create a task with it and start it up. Shown here:

BlockingCollection<Stream> bcs = new BlockingCollection<Stream>();                   
foreach (Stage s in pipeline.Stages) 
{
    IParallelPipe p = (IParallelPipe)Factory.GetPipe(s.type);
    Task.Factory.StartNew(() => p.Process(ref bcs, s.id)); 
}

In each run of this in my sample, pipeline.Stages contains two elements, one that gets instantiated as class A and the other as class B. This is fine, I see it in te debugger as p coming away with the two different types. However, class B never gets called, instead I get two invocations of the A.Process(…) method. Both contain the stageId for the that was passed in (ie. the two invocations have different stageIds).

Now, if I take and separate things out a bit, just for testing I can get things to work by doing something like this:

BlockingCollection<Stream> bcs = new BlockingCollection<Stream>();                   
A a = null;
B b = null;
foreach (Stage s in pipeline.Stages) 
{
    IParallelPipe p = (IParallelPipe)Factory.GetPipe(s.type);
    if(p is A)
        a = p;
    else
        b = p;
}
Task.Factory.StartNew(() => a.Process(ref bcs, idThatINeed)); 
Task.Factory.StartNew(() => b.Process(ref bcs, idThatINeed));

This invokes the appropriate class!

Any thoughts???

  • 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-12T22:08:54+00:00Added an answer on May 12, 2026 at 10:08 pm

    The behaviour you’re describing seems odd to me – I’d expect the right instances to be used, but potentially with the wrong stage ID – the old foreach variable capture problem. The variable s is being captured, and by the time the task factory evaluates the closure, the value of s has changed.

    This is definitely a problem in your code, but it doesn’t explain why you’re seeing a problem. Just to check, you really are declaring p within the loop, and not outside it? If you were declaring p outside the loop, that would explain everything.

    Here’s the fix for the capture problem though:

    BlockingCollection<Stream> bcs = new BlockingCollection<Stream>();
    foreach (Stage s in pipeline.Stages) 
    {
        Stage copy = s;
        IParallelPipe p = (IParallelPipe)Factory.GetPipe(s.type);
        Task.Factory.StartNew(() => p.Process(ref bcs, copy.id)); 
    }
    

    Note that we’re just taking a copy inside the loop, and capturing that copy, to get a different “instance” of the variable each time.

    Alternatively, instead of capturing the stage, we could just capture the ID as that’s all we need:

    BlockingCollection<Stream> bcs = new BlockingCollection<Stream>();
    foreach (Stage s in pipeline.Stages) 
    {
        long id = s.id;
        IParallelPipe p = (IParallelPipe)Factory.GetPipe(s.type);
        Task.Factory.StartNew(() => p.Process(ref bcs, id)); 
    }
    

    If that doesn’t help, could you post a short but complete program which demonstrates the problem? That would make it a lot easier to track down.

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

Sidebar

Related Questions

I'm playing around with a composite control that uses the ASP.NET templating system. I
I've been playing around with UniObjects .NET. I would like to be able to
I have been playing around with Threads and Tasks (.net 4) and noticed some
I am playing around with the new System.ComponentModel.Composition namespace in .NET 4.0 beta 2,
I'm playing around with .net 4's System.Windows.Markup.XamlReader - just as an education exercise -
I am trying to learn asp.net 4 web app but playing around the web
I am playing around with ASP.NET MVC for the first time, so I apologize
I'm playing around with ASP.NET MVC and I just loaded up an empty project
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
I am currently playing around with the Asp.Net mvc framework and loving it compared

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.