Threading Problems in Windows Form

Asked

Viewed 39 times

2

I’m trying to implement Threads in my algorithm, however when adding and running my software the Windows Form of it hangs and does not perform any action, can you see anything wrong in the code below? I know there’s a Async/Await to be used with the task, but it is possible to use Threads as I used, on Windows Form? If not, what would be the best way to add a pool of task that would "take" at least 90% of my processor to run the algorithm?

Thanks in advance!

    GeneticAlgorithm AG = new GeneticAlgorithm();
    List<Thread> threads = new List<Thread>();

    for (i = iTemp; i < evolucoes; i++)
    {
        Thread thread_corrente = new Thread(() =>
        {
            #region
            iTemp++;

            lbEvolucoes.Text = i.ToString();
            lbEvolucoes.Refresh();

            pop = AG.executeGA(pop);

            if (twoOptCheck.Checked)
            {
                if (i == evolucoes - 1)
                    Utils.TwoOpt(pop.getBest());
            }
            pop.getBest().CalcFitness();

            //Limpar o grafico
            zedMedia.GraphPane.CurveList.Clear();
            zedMedia.GraphPane.GraphObjList.Clear();

            double mediaPop = pop.getMediaPop();
            mediaPopulacao.Add(i, mediaPop);

            double bestFitness = pop.getBest().getFitness();

            lbMenorDistancia.Text = bestFitness.ToString("0.0");
            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();
            #endregion
        });

        thread_corrente.Start();
        threads.Add(thread_corrente);
        if (threads.Count >= 10)
        {
            while (!ThreadsTerminaramDeProcesssar(threads))
            {
                Thread.Sleep(5000);
            }

            threads.Clear();
        }
    }

    g.Clear(Color.White);
    plotLines(pop, Color.Blue);
    plotPoints();

    btnExecutar.Enabled = true;
}

Checks if Thread has already processed:

   private bool ThreadsTerminaramDeProcesssar(List<Thread> threads)
    {
        bool TodosTerminaramDeProcessar = true;

        foreach (Thread thread in threads)
        {
            //true => não terminou seu processamento.
            //false => terminou seu processamento.
            if (thread.IsAlive)
                TodosTerminaramDeProcessar = false;
        }

        return TodosTerminaramDeProcessar;
    }
  • Be able to use thread direct, can, but should not. The main reason is because it is very difficult to do right. Using Task it is difficult, but most mistakes will not be made. Debug thread is probably the most complicated thing in computing. And that’s what you’re asking for. I won’t do it for you, even though the problem may be simple in this case, and I don’t want to waste any time. You probably even have a better way of doing all the code. I already see weird things there, it seems that you want to recreate the Task already does for you.

  • Every time I see one Thread.Sleep() I’m pretty sure there’s something wrong with the same code. This code is too confusing, the real solution is to do it better, it doesn’t seem to be a one-off problem that we can help.

No answers

Browser other questions tagged

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