Skip to content

Work Dependency

ZjzMisaka edited this page Nov 19, 2024 · 8 revisions

If WorkOption.Dependents is set, the work will not start until all dependent works have completed execution.

string id0 = powerPool.QueueWorkItem(() =>
{
    //Do something
});
string id1 = powerPool.QueueWorkItem(() =>
{
    //Do something
});

powerPool.QueueWorkItem(() =>
{
    //Do something
},
new WorkOption()
{
    Dependents = new ConcurrentSet<string>() { id0, id1 }
});

The thread pool will only save the information of works that have failed, and the record of failed works will be cleared when the thread pool enters the Idle state and is restarted, unless the PowerPoolOption.ClearFailedWorkRecordWhenPoolStart is set to false.
Normally, if a work begins execution but the work it depends on fail to complete, then it should not be allowed to execute.
If PowerPoolOption.ClearFailedWorkRecordWhenPoolStart is true, to prevent the loss of failure informations leading to the thread pool erroneously allowing the work to start, one can use PowerPool.EnablePoolIdleCheck to temporarily halt the thread pool's idle check until this work has been added to the thread pool.