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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:05:02+00:00 2026-05-27T08:05:02+00:00

using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public sealed class Foo { public int ID;

  • 0
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public sealed class Foo
{
    public int ID;

    private static readonly Dictionary<int, Foo> Instances = new Dictionary<int, Foo>();

    public static void FillInstances()
    {
        // here's some magic code to fill .Instances
    }

    public static void DoSomething()
    {
        var tasks = new Task[Instances.Count];
        var i = 0;
        foreach (var foo in Instances.Values)
        {
            var instance = foo; // just to break the reference
            var task = Task.Factory.StartNew(instance.DoSomethingInternal);
            tasks[i++] = task;
            Logger.DebugFormat("translation: task #{0} = id {1}", task.Id, instance.ID);
        }
        using (var timer = new Timer(callback =>
        {
            Logger.DebugFormat("--------------------- {0}", DateTime.Now);
            foreach (var task in tasks)
            {
                var taskInstance = task; // just to break the reference
                Logger.DebugFormat("task #{0}: {1}", taskInstance.Id, taskInstance.Status);
            }
        }, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)))
        {
            Task.WaitAll(tasks);
            Logger.Debug("ready");
        }
    }

    private void DoSomethingInternal()
    {
        Logger.DebugFormat("DoSomethingInternal / task #{0} - id {1}", Task.CurrentId, this.ID);
    }
}

The itention should be clear: I’m calling a static method DoSomething to call instance-methods DoSomethingInternal parallel.

To output of my console is:

translation: task #1 = id 9
translation: task #2 = id 5
translation: task #3 = id 2
translation: task #4 = id 3
translation: task #5 = id 7
translation: task #6 = id 8
translation: task #7 = id 1
translation: task #8 = id 10
DoSomethingInternal / task #8 - id 10
DoSomethingInternal / task #7 - id 1
DoSomethingInternal / task #6 - id 8
DoSomethingInternal / task #5 - id 7
DoSomethingInternal / task #4 - id 3
--------------------- 12/01/2011 08:29:57
task #1: Running
task #2: Running
task #3: Running
task #4: RanToCompletion
task #5: RanToCompletion
task #6: RanToCompletion
task #7: RanToCompletion
task #8: RanToCompletion
--------------------- 12/01/2011 08:29:58
task #1: Running
task #2: Running
task #3: Running
task #4: RanToCompletion
task #5: RanToCompletion
task #6: RanToCompletion
task #7: RanToCompletion
task #8: RanToCompletion
--------------------- 12/01/2011 08:29:59
task #1: Running
task #2: Running
task #3: Running
task #4: RanToCompletion
task #5: RanToCompletion
task #6: RanToCompletion
task #7: RanToCompletion
task #8: RanToCompletion

Obviously task #1, #2, #3 never get started, but .Status of this System.Threading.Tasks.Task-instances tells us that it’s Running.
So what’s the problem here?

  • 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-27T08:05:03+00:00Added an answer on May 27, 2026 at 8:05 am

    I would say you have a deadlock somewhere in DoSomethingInternal your output looks far to linear to me.

    When I run your code, using Console.WriteLine for both logger methods, the output is:

    translation: task #1 = id 0
    DoSomethingInternal / task #1 - id 0
    translation: task #2 = id 1
    translation: task #3 = id 2
    DoSomethingInternal / task #2 - id 1
    DoSomethingInternal / task #3 - id 2
    DoSomethingInternal / task #4 - id 3
    translation: task #4 = id 3
    translation: task #5 = id 4
    DoSomethingInternal / task #5 - id 4
    DoSomethingInternal / task #6 - id 5
    translation: task #6 = id 5
    translation: task #7 = id 6
    translation: task #8 = id 7
    DoSomethingInternal / task #7 - id 6
    DoSomethingInternal / task #8 - id 7
    translation: task #9 = id 8
    translation: task #10 = id 9
    DoSomethingInternal / task #10 - id 9
    DoSomethingInternal / task #9 - id 8
    ready
    

    I can get a similar output as you do by locking the same object in DoSomething and DoSomethingInternal although in my case it is the first 7 which don’t complete. running DoSomething on a ThreadPool thread

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

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadDemo { class
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Threading; namespace ClassLibrary { public
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication1 { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public
I have this code (C#): using System.Collections.Generic; namespace ConsoleApplication1 { public struct Thing {
I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
This is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;

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.