C# mutiple threads block RRS feed

  • Question

  • Hi Experts,

    I have a windows service created by C#. This service will lunch new task based on the schedule user specified. But the problem is: sometime, the task A started, all other tasks started later were waing for this one. Once A finished other tasks will finished. Similar to FIFO.

    We start the task like this:

    Task.Factory.StartNew(job.Run, TaskCreationOptions.LongRunning);

    But what I want is no relevance. All tasks created and finished separetly. I don't want to see dependence among the tasks.

    Please help me on this. Thank you!

    • Edited by SSAS_user Monday, April 9, 2012 8:29 AM
    Monday, April 9, 2012 8:28 AM


All replies

  • You can use Task.ContinueWith function to start a task when one finishes.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, April 9, 2012 8:32 AM
  • Sorry I didn't get you. Mutiple tasks may have to be started at the same time. If the user specify start time of 2 tasks at the same time, then they have to be started at the same time. We have more than thousands of tasks one day. The tasks may spend 1 min but it may also took a few hours.
    • Edited by SSAS_user Monday, April 9, 2012 8:39 AM
    Monday, April 9, 2012 8:37 AM
  • We'd need to see more of your code to see if you're introducing an unintended dependency yourself.  It's also possible that all of the tasks are bottle-necking because they need a shared resource; maybe they all need the same file and one has a lock, maybe they're all IO bound on the hard disk and that disk can't keep up; maybe they're all performing mostly network communication and there's a bottleneck there.

    You could try to use a profile to see where various tasks are 'stopping' and look there for bottlenecks.  If the other tasks aren't getting started at all then it means there's a bug or problem in your scheduling code.

    Monday, April 9, 2012 1:57 PM
  • Hi, 

    When you use Task, it uses Thread pool threads to work on. On starting of task, it simply queue the delegate to be picked by thread pool thread. so there is no guarantee in order. As you have mentioned there are some tasks take hours to finish, so there is a chance of blocking thread pool threads this might prevent other tasks from starting.

    It is hard to imagine what is happening with out seeing all the details. like

    How you starting all tasks? in time interval?

    What action you are performing in Task execution?

    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Monday, April 9, 2012 5:49 PM
  • Thanks. We are using timmer to start the task every minute. Each task is an Excel Job, we are auto refreshing Excel files. I start the tasks like this:

    Task.Factory.StartNew(job.Run, TaskCreationOptions.LongRunning);

    I saw the job A started, and then other jobs started later, other job finished process but they are waiting for the A. Only A finish the job, and they release the resource at the same time. Thank you!

    Tuesday, April 10, 2012 1:37 AM
  • How about trying to write a method to block other tasks besides task A?

    My idea is as below:

    void waitTaskFinish()
      while (m_objTask.IsAlive()) // m_objTask is task A here
    // other ations
    // actions when task A is finished

    You can add this method before you create other tasks.

    BTW, maybe you list your detail codes here will be more helpful. :)




    Version Control Software| TWAIN products 

    Wednesday, April 11, 2012 3:11 AM
  • Sorry. I don't want to see any task block other ones. All the tasks should be started and finished seperated. That's the purpose. But right now, I saw some tasks blocked other task similar to FIFO, but I don't want that. It seems I didn't make it clear, any question please let me know.

    Wednesday, April 11, 2012 5:55 AM
  • SSAS,

    It seems if asynchronous and synchronous are not completely clear to you.

    I do it with an example. 

    10 persons start eating; and then you order that they should all finished eating everything on their plate at the same time.

    That you cannot do, the same with like you describe it in my idea. 

    This text is not made because you made a typo in "Lunch" and "Launch".




    Wednesday, April 11, 2012 6:53 AM
  • Thanks for all your responses. Ok, you can ingore previous descriptions. Here is one example what I want:

    10 tasks started based on the schedule.  Task 1 start time: 1:00 AM, Other 9 tasks start time: 1:05 AM.

    Task 1 may took more than 1 hour to finish because it has huge dataset. Other 9 tasks can be finished within 1 minute because they are simple task I know that. Task A started firstly based on the schedule, and hope other 9 tasks can be finished without waiting task 1.


    Now all 10 tasks started correctly, but the problem is they finished at the same time. other 9 tasks were waiting for task 1 due to unknown reason... how to fix that.



    • Edited by SSAS_user Wednesday, April 11, 2012 7:33 AM
    Wednesday, April 11, 2012 7:29 AM
  • 10 persons start eating; and then you order that they should all finished eating everything on their plate at the same time.

    When they finished the lunch they should leave. Now the problem is: all 10 persons were waiting the first person. They leave at the same time.....I don't want to order them finish at the same time.



    • Edited by SSAS_user Wednesday, April 11, 2012 7:38 AM
    Wednesday, April 11, 2012 7:36 AM
  • Yea but why do you then use a task factory. In my idea they don't have to wait on each other to start eating when you have guided them to their chair and (maybe only to inform you when they leave).

    Even the backgroundworker does this job.  

    On first sight it looks if you have added complexity which you don't need.

    Be aware that the maximum threads concurrently active is limited.


    Wednesday, April 11, 2012 8:16 AM