Parallel Execution

0

I have a process that is 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 processing of instruction block 1 ends up being delayed, and instruction block 2 ends up being 'hostage' to this delay.

I would like as long as instruction block 1 was executed, instruction block 2 was also executed, how many times it is called by timer .

  

The code executed in block 2 does not depend on block 1.

    
asked by anonymous 10.10.2018 / 20:41

1 answer

0

You can create an array of tasks, and use the command Task.WaitAll to wait for all of them 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());
    
10.10.2018 / 21:33