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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:37:15+00:00 2026-05-25T11:37:15+00:00

Yesterday on SO, i saw a thread asking for a code which some what

  • 0

Yesterday on SO, i saw a thread asking for a code which some what does this way. I mean, you (manager thread) start number of tasks using TPL APIs and once they are completed job, that thread should notify you (Manager) back who maintains Task pool.

So here is the code which i tried. Although i must say it works as i describe above.

class TaskJob
{
    public delegate void NotificationDelegate(int? taskId,string message);
    public event NotificationDelegate NotifyCompletion;

    public void TaskToRun()
    {
        try
        {
            if (Task.CurrentId == 4)//If its a 4th thread, simulate exception
                throw new Exception();

            Console.WriteLine("Task started with thread id " + Task.CurrentId);
            Thread.Sleep(100000);

            Console.WriteLine("Task finished with thread id " + Task.CurrentId);

            NotifyCompletion(Task.CurrentId, "Successfully completed");
        }
        catch
        {
            NotifyCompletion(Task.CurrentId, "Faulted error");
        }
    }        
}

class Program
{
    static List<Task> taskList = new List<Task>();
    public static void Main()
    {
        for (int i = 0; i < 5; i++)//starting 5 threads/tasks
        {
            TaskJob tb = new TaskJob();
            tb.NotifyCompletion += new ConsoleApplication1.TaskJob.NotificationDelegate(tb_NotifyCompletion);
            Task t = Task.Factory.StartNew(tb.TaskToRun);
            taskList.Add(t);
        }

        Task.WaitAll(taskList.ToArray());
        CheckAndDispose();

        Console.ReadLine();

    }

    private static void CheckAndDispose()
    {
        foreach (var item in taskList)
        {
            Console.WriteLine("Status of task = " + item.Id + " is = " + item.Status);
            item.Dispose();
        }
    }

    static void tb_NotifyCompletion(int? taskId, string message)
    {
        Console.WriteLine("Task with id completed ="+ taskId + " with message = " + message);            
    }
}

Couple of heads up:

  1. Dont worry about not maintaining Task[] rather than having a list and then converting to array. Its just a code. Not that i am concentrating on efficiency.
  2. Am not worried on custom Dispose implementation as of now.

Now i am asking myself couple of questions, but fail to convince myself with an suitable answer. Here are they:

  1. Is this way solving the problem good? Or is there a better way to do it? Code please 🙂
  2. How to make sure that the tasks objects are really disposed (upon calling Dispose) but not implementing the custom Dispose pattern.
  3. Have not done any memory leak test using tools. But just visual sake, do you see leaks?
  4. In the for loop (main method) i am creating TaskJob class objects which are local to loop. Thus these objects gets gc’d (lets say out of scope) after loop completion. So, how the event is fired back which i am hooking up using this object but it has been disposed after loop when actually the event is fired.
  5. Any thing else you wish to add?

Thanks alot 🙂

  • 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-25T11:37:16+00:00Added an answer on May 25, 2026 at 11:37 am

    There’s no need to do this yourself at all – use continuations with Task.ContinueWith or Task<T>.ContinueWith. That basically lets you say what to do when a task completes – including executing different code on failure, cancellation, and success.

    It also returns a task, so you can then continue when that’s completed etc.

    Oh, and you can give it a TaskScheduler, so that from a UI thread you can say, “When this background task finishes, execute the given delegate on the UI thread” or similar things.

    This is the approach that C# 5 async methods are built on.

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

Sidebar

Related Questions

Ok, I was scanning through some code yesterday and I saw this, Code: public
Yesterday I read some code of a colleague and came across this: class a_class
Yesterday i saw a site in which, site is listing the ip address and
I saw a code snippet yesterday in one of the responses here on StackOverflow
Yesterday, I found myself writing code like this: SomeStruct getSomeStruct() { SomeStruct input; cin
when I first saw C#, I thought this must be some joke. I was
I saw a picture in @Matthieu's blog http://wp7wonders.wordpress.com/ yesterday(sorry,I can't find it now. This
I saw some interesting compiler behavior yesterday and I think I understand why it's
I was looking through some of the .net source yesterday and saw several implementations
After several plugins got updated yesterday I saw in the source code that actually

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.