I have a method button_click which runs a task and another that cancels it. The problem is that when trying to run the task again it throws an exception: 'Start can not be invoked in a task that has been completed.'
This is my code:
private void Iniciar_Click(object sender, EventArgs e)//boton de inicio
{
bstate = bstate * (-1);
Acomodar_cajas();
ct = ts.Token;
Task ta;
if (bstate == 1)//detener
{
iniciar.Text = "Iniciar";
ts.Cancel();
td_cb.Enabled = true;
Desbloquear_grupo_compas();
indice_caja = 0;
Limpiar_cajas();
check_box_td_state();
}
else//iniciar
{
ta = new Task (() => Tarea(), ct);
ta.Start();
iniciar.Text = "Detener";
get_values();//obtiene los valores de las cajas de texto
bloquear_grupo_td();//bloquea grupo td
bloquear__grupo_compas();
}
}