I have a problem with my application, where due to the large volume of processing, my Form
hangs. During my research I discovered that a async
method solves this, but the functions executed on my "execute" button return void, so I can not use await
because I do not know when the execution of the function will end (since it does not return a task).
Ah some way to use Thread or Task in this scenario? Unlocking form and splitting processing?
EDIT AFTER SUGGESTIONS:
My for
looks like this after the suggestions:
for (i = iTemp; i < evolucoes; i++)
{
iTemp++;
lbEvolucoes.Text = i.ToString();
lbEvolucoes.Refresh();
await Task.Factory.StartNew(() =>
{
pop = AG.executeGA(pop);
});
// pop = AG.executeGA(pop);
//Limpar o grafico
zedMedia.GraphPane.CurveList.Clear();
zedMedia.GraphPane.GraphObjList.Clear();
double mediaPop = pop.getMediaPop();
mediaPopulacao.Add(i, mediaPop);
double bestFitness = pop.getBest().getFitness();
#region Começo do dois opt
await Task.Factory.StartNew(() =>
{
var tasks = new List<Task>();
for (int j = 0; j < 100; j++)
tasks.Add(Task.Run(() =>
{
Utils.TwoOpt(pop.getBest());
}));
});
#endregion
lbMenorDistancia.Text = bestFitness.ToString();
lbMenorDistancia.Refresh();
LineItem media = paneMedia.AddCurve("Média", mediaPopulacao, Color.Red, SymbolType.None);
//Print linhas a cada 6 evolucoes
if (i % 6 == 0 && bestFitness < bestAux)
{
bestAux = bestFitness;
g.Clear(Color.White);
plotLines(pop, Color.Blue);
plotPoints();
}
zedMedia.AxisChange();
zedMedia.Invalidate();
zedMedia.Refresh();
}
It's this way, is it legal?