I have several always running threads which are executing "tasks" and one thread which gets new "tasks" and distributes between these threads.
Thread which executes task:
public class TaskExecutor
{
public object CurrentTask { get; set; }
public Thread ThreadRef { get; set; }
private void Run()
{
while (true)
{
try
{
if (CurrentTask != null)
ExecuteTask(CurrentTask);
else
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException)
{
}
catch (Exception)
{
CurrentTask = null;
}
}
}
private void ExecuteTask(object task)
{
}
}
Thread which distributes tasks:
private void TryDistribute(TaskExecutor thread, object task)
{
if (thread.CurrentTask == null)
{
thread.CurrentTask = task;
thread.ThreadRef.Interrupt();
}
}
How to implement the same logic (indefinite wait and waking up for processing by other thread) using TPL Task?