I have a loop of repetition that does several iterations and has in its scope calculations that require a lot of processing.
The problem is that when running the code snippet, processor usage will gradually rise, until the application crashes and then the operating system later.
Well, my goal is to limit somehow the consumption of these features that lead to system crashes. I know this will increase response time, but my priority at the moment is not to crash the system.
The project is in Windows Forms , but this snippet is in a Class Library .
See a snippet of code:
private static string[] TodosPossiveis(char[,] letras)
{
int linhas = letras.GetUpperBound(1) + 1;
int colunas = letras.GetUpperBound(0) + 1;
int max = (int)Math.Pow(linhas, colunas);
string[] todos = new string[max];
int[] posY = new int[colunas];
int atual = 0;
while (atual < max)
{
string nova = "";
for (int i = 0; i < colunas; i++)
{
nova += letras[i, posY[i]];
}
for (int i = colunas - 1; i > -1; i--)
{
posY[i]++;
if (posY[i] == linhas)
{
posY[i] = 0;
}
else
{
break;
}
}
todos[atual++] = nova;
}
return todos;
}
This method receives a character set and returns all possible combinations.
I tried to use only primitive variables to improve performance, but even so, receiving a large character set, the system crashes.