In C # .Net 3.5, how can I manage multiple WaitHandles?

I have the following scenario:

My application should import multiple datasets from another application, and time is critical.
Because of this, it generates one thread for each import.
So let's say I have import 1 througth 10 where import 4 and 5 can only start after import 1, 6 and 7 after import 2 and 8, 9 and 10 after import 3

  • Import 1
    • Import 4
    • Import 5
  • Import 2
    • Import 6
    • Import 7
  • Import 3
    • Import 8
    • Import 9
    • Import 10

stackoverflow waithandles, , waithandles.
/ , , .set().
, , - , .

, ?

UPDATE:
, , :
dateSince = Convert.ToDateTime(txtBoxDateSince.Text); dateTo = Convert.ToDateTime(txtBoxDateTo.Text);

//Loop all the days on the time interval while (DateTime.Compare(dateSince, dateTo) <= 0) {

foreach (ListItem father in checkBoxListFather.Items)
{
    if (father.Selected == true)
    {
        processClass process = new processClass();

        // This WaitHandle will be used to get the child tasks going.
        var wh = new ManualResetEvent(false);

        //Method to Import, wraped in a delegate
        WaitCallback fatherMethod = new WaitCallback(process.importProcess);
        //and its parameters
        processClass.importParameters param = new processClass.importParameters(wh, father.Value, null, dateSince);

        // Queue the parent task.
        ThreadPool.QueueUserWorkItem(fundMethod, param);

        // Register the child tasks.
        foreach (ListItem child in checkBoxListChild.Items)
        {
            if (child.Selected == true)
            {
                processClass.importParameters parameters = new processClass.importParameters(null, child.Value, null, dateSince);

                // Registers a callback for the child task that will execute once the
                // parent task is complete.
                WaitOrTimerCallback childMethod = new WaitOrTimerCallback(process.anotherImportProcess);
                RegisteredWaitHandle rwh = ThreadPool.RegisterWaitForSingleObject(wh, childMethod, parameters, Timeout.Infinite, true);

            }//End if (child.Selected == true)

        }//End foreach (ListItem fund in checkBoxListChild.Items)

    }//End if (father.Selected == true)

}//End foreach (ListItem fundProcess in checkBoxListFather.Items)

dateSince = dtSince.AddDays(1);

}// while (DateTime.Compare(from, to) < 0)

, - .
, .

.

+3
2

, WaitHandle . 1 Thread, Join, , .

void TaskEntryPoint(Thread parent, ImportTask task)
{
  if (parent != null)
  {
    parent.Join(); // Wait for the parent task to complete.
  }
  task.Execute(); // Execute the child task.
}

, , , . TaskEntryPoint pass null, Thread .

Update:

, ThreadPool. ThreadPool.RegisterWaitForSingleObject. , , , WaitHandle .

foreach (ImportTask parent in parentTasks)
{
    // This WaitHandle will be used to get the child tasks going.
    var wh = new ManualResetEvent(false);

    // Needed to capture the loop variable correctly.
    var p = parent; 

    // Queue the parent task.
    ThreadPool.QueueUserWorkItem(
        (state) =>
        {
            try
            {
                // Execute the parent task.
                p.Execute();
            }
            finally
            {
                // Signal the event so that the child tasks can begin executing.
                wh.Set();
            }
        }, null);

    // Register the child tasks.
    foreach (ImportTask child in parent.ChildTasks)
    {
        // Needed to capture the loop variable correctly.
        var c = child;

        // Registers a callback for the child task that will execute once the
        // parent task is complete.
        RegisteredWaitHandle rwh = ThreadPool.RegisterWaitForSingleObject(wh,
            (state, to) =>
            {
                // Execute the child task.
                c.Execute();
            }, 
            null, Timeout.Infinite, true);
    }
}

RegisterWaitForSingleObject . , , . , . , , , .


1 . ThreadPool Task?

+1

All Articles