Application optimization doubt

0

I did a process in c # which is running all the time, the problem is using a lot of CPU percentage, does anyone know how to reduce that percentage?

do
            {
                try
                {

                    LogClasifica("Configurando Directorios...");  
                    lector.obtenFiles(fields);
         }
                catch(Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
            while (true);




    public void obtenFiles(string[] field)
    {
        try
        {

            List<string> dirsRaiz = new List<string>(Directory.EnumerateDirectories(field[1]));

            foreach (var dir in dirsRaiz)
            {

                if (dir.Contains(field[21]) || dir.Contains(field[22]) || dir.Contains(field[23]))
                {

                    String dirPath2 = dir;
                    List<string> dirs2 = new List<string>(Directory.EnumerateDirectories(dirPath2));

                    foreach (string dir2 in dirs2)
                    {

                        if (dir2.Contains("AUX"))
                        {
                            //LogClasifica(dir2);
                            int subString2 = Convert.ToInt16(field[45]);
                            string string3 = dir2.Substring(subString2);
                            n = Convert.ToInt16(string3);
                            if (n >= Convert.ToInt16(field[37]) && n <= Convert.ToInt16(field[38])) //CONDICION PARA USESAP
                            {
                                if (dir.Contains(field[21]))
                                {
                                    if (!Directory.Exists(field[10]))
                                    {
                                        Directory.CreateDirectory(field[10]);
                                    }

                                    CopyDirectoryContent2(dir2.Substring(0, subString2) + n, field[10], true);
                                }
                                if (dir.Contains(field[22]) || dir.Contains(field[23]))
                                {
                                    if (!Directory.Exists(field[11]))
                                    {
                                        Directory.CreateDirectory(field[11]);
                                    }
                                    CopyDirectoryContent2(dir2.Substring(0, subString2) + n, field[11], true);
                                }
                            }
                        }
                    }    
                }

            }
        }
        catch (Exception err)
        {
            LogClasifica("Error:" + err.Message + err.StackTrace);
        }

    }
    
asked by Cesar Ramirez Monroy 09.03.2016 в 20:11
source

2 answers

3

You have several problems, especially code building.

For the theme of the performance, a solution like @leandro's would be good for you, and I was going to propose this to you

const int WAIT = 1000;//1 seg, pero puede ser menos o mucho menos segun lo que requieras

do
{
   logClasifica("Configurando Directorios...");
   lector.obtenFiles(fields);
   Thread.Sleep(WAIT);
}
while (true);

With this you gain several improvements

  • you give a break to the IO especially for the writes on disk, having a wait loop that writes to disk without stopping is a ** bad ** idea
  • Do not do an exception control as big, better handle exceptions where relevant in the other methods. an exception handling the way you have it is TOO expensive.
  • But ..

    Your code is very messy, I would recommend encapsulating the logic in a different method or class. In general, a method as long as you have is a sign that you have too many responsibilities, which means that you do not have your solution well modeled.

    A class: a responsibility, a method, a responsibility ... fragment, group and order functionalities.

        
    answered by 09.04.2016 / 14:24
    source
    1

    The truth is that I see little to optimize.

    The part of the process of the directories in the method obtenFiles() is very particular to the logic you need, I would not know how to change that part without understanding the background you need to perform.

    If I can mention that you do not use a do..while to repeat the cycle, implement a timer that runs every so often.

    Timer t1 = null;
    
    public void Form1_Load(...){
    
        t1 = new Timer();
        t1.Interval = 1000;
        t1.Tick += t1_Tick;
    
        t1.Start();
    
    }
    
    public void t1_Tick(...){
    
        t1.Stop();
    
        LogClasifica("Configurando Directorios...");  
        lector.obtenFiles(fields);
    
        t1.Start();
    }
    

    You could also evaluate to launch the process in thread (thread), so that it does not block the application. For this you could use the timer that is in System.Threading

    Thread Timers (Visual C # and Visual Basic)

        
    answered by 10.03.2016 в 12:34