12
I have a loop loop that makes several iterations and has in its scope calculations that require a lot of processing.
The problem is that when executing the code snippet, the use of the processor increases gradually, until locking the application and later the operating system.
Well, my goal is to somehow limit the consumption of those resources that lead to a system crash. I know this will increase response time, but my priority at the moment is not to shut down the system.
The project is in Windows Forms, but this stretch is in a Class Library.
See an excerpt from the 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 takes a character set and returns all possible combinations.
I tried to use only primitive variables to improve performance, but even so, when receiving a large set of characters the system ends up crashing.
Have you considered processing into a separate thread? With this he would use the processor, but still give space to other processes of the system and of all OS.
– Ricardo
The operation locks the operating system or its application?
– Jéf Bueno
Try using a Thread.Sleep(10); so the system pauses and does not consume as much processing.
– Rafael Lincoln
@Ricardo how could I do that? Could I formulate an answer so that I accept?
– Jedaias Rodrigues
@jbueno everything starts with the application not responding, but in a short time the whole operating system gets stuck.
– Jedaias Rodrigues
@Jedaiasrodrigues you use running this as? Winfor, Asp.net?
– Ricardo
@Ricardo Windows Form, I’ll add in question.
– Jedaias Rodrigues
Try to follow this guide, I needed to do a heavy processing here and with it I not only released the processes but did faster by using several only same time. If you have problems with the guide that you put the problem with your code that I help the most. https://msdn.microsoft.com/pt-br/library/ywkkz4s1.aspx
– Ricardo