0
I have a problem in my application, where due to the large volume of processing, my Form
lock. During my researches I found that a method async
resolves this, however the functions performed on my "run" button return void, this way I can not use the await
because I do not know when the execution of the function will end(because it does not return a task).
Is there any way to use Thread or Task in this scenario? Unlocking the form and dividing the processing?
EDIT AFTER SUGGESTIONS:
Man for
was this way 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 like this, it’s cool?
You’re not getting back one
async void
? Another solution is to use theTask.Factory
– Francisco
Then no use returning a void, because I would have to tell my form to stay working while the function does not finish processing , this I would do with the await, but with void return it is not possible to call the Await, because the function needs to return a Task saying that it is over, Factory tmb did not give :/
– Guilherme Prado
Make the calling method
async
and putawait
before the call to a method does not cause the code of the called method to be executed asynchronously. One possible way is to useTask.Factory.StartNew(metodo())
to call him.– ramaral