Parallel execution

Asked

Viewed 29 times

0

I have a process running in my application for control. This process runs within a timer.

private void timer1_Tick(object sender, EventArgs e)
{
    // bloco de instruções 1

    // bloco de instruções 2
}

What happens is that the processing of the instruction block 1 ends up being delayed, and the instruction block 2 ends up being 'hostage' of this delay.

I would like as long as instruction block 1 is executed, instruction block 2 is also executed, as often as it is called by timer.

Code executed in block 2 does not depend on block 1.

1 answer

0

You can create an array of tasks, and use the command Task.WaitAll to wait for all to finish.

var tarefas = new List<Task>()
{
    Task.Run(() => {
        // Bloco de código 1
    }),
    Task.Run(() => {
        // Bloco de código 2
    })
};

Task.WaitAll(tarefas.ToArray());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.