Click here to Skip to main content
16,011,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a system that looks in a client informed folder for OpenFileDialog for all XML, then reads them, and then imports the data into a SQL Server database.
The problem is that after a while, it stops importing, and the system hangs, but in monitoring everything is still running. If I import one by one, I can import them all, but the system must be importing hundreds of XML at a time.
Another problem is a method that returns true or false when XML has already been imported, if I start debugging, the method returns false, if I let it run without delegation, it returns true.


What I have tried:

    //Counts how many files there are in the path of the first XML folder
    int cte = 0;
    if (Directory.Exists(caminhocte))
    {
        foreach (string filePath in Directory.EnumerateFiles(caminhocte, "*.xml", SearchOption.AllDirectories))
        {
            cte++;
        }
    }
    //Counts how many files there are in the path of the second XML folder
    int nfe = 0;
    if (Directory.Exists(caminhonfe))
    {
        foreach (string filePath in Directory.EnumerateFiles(caminhonfe, "*.xml", SearchOption.AllDirectories))
        {
            nfe++;
        }
    }

    //Displays a message with how much XML will be imported, and asks if he wants to continue with the import
    MessageBoxButtons buttonn = MessageBoxButtons.YesNo;
    MessageBoxIcon iconn = MessageBoxIcon.Information;
    string messagen = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}",
        "Detalhes: ", Environment.NewLine,
        "Numero de XML na pasta CTE: ", cte, Environment.NewLine,
        "Numero de XML na pasta NFE: ", nfe, Environment.NewLine,
        "Deseja continuar com a importação?");
    string titlen = "Aviso";
    DialogResult result = MessageBox.Show(messagen, titlen, buttonn, iconn);
    //If you do not want to continue importing, the system returns
    if (result == DialogResult.No)
    {
        return;
    }


    int ctes = 0;
    int ctee = 0;
    if (Directory.Exists(caminhocte))
    {
        ImportaXML(caminhocte, "CTE");
        ctes = xmlimportsucess;
        ctee = xmlimporterror;
    }
    int nfes = 0;
    int nfee = 0;
    if (Directory.Exists(caminhonfe))
    {
        ImportaXML(caminhonfe, "NFE");
        nfes = xmlimportsucess;
        nfee = xmlimporterror;
    }
    xmlimporterror = 0;
    xmlimportsucess = 0;

    //Build a message that will be generated at the end of the process in txt form.
    string messagesucess = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}",
        "Informações da importação do CTE", Environment.NewLine,
        "CTE importado com sucesso: ", ctes, Environment.NewLine,
        "CTE não importado(Erro): ", ctee, Environment.NewLine,
        "Informações da importação da NFe", Environment.NewLine,
        "NFe importada com sucesso: ", nfes, Environment.NewLine,
        "NFe não importado(Erro): ", nfee, Environment.NewLine,
        "-----------------------------------"
        );

    MessageBoxButtons buttonF = MessageBoxButtons.OK;
    MessageBoxIcon iconF = MessageBoxIcon.Information;
    string messageF = "Arquivo de Registro do log foi gerado com sucesso.";
    string titleF = "Aviso";
    MessageBox.Show(messageF, titleF, buttonF, iconF);

    //Create folder and log txt file
    registrarLog(messagesucess, "", "Reg");
    GC.Collect();




//This method will get the path and type case, in the DirectXML class it will read the XML and see if it is a CTE or NFE and will import the XML information.
//If it gives an error, it will log a log.
void ImportaXML(string caminho, string tipo)
{
    xmlimportsucess = 0;
    xmlimporterror = 0;
    foreach (string filePath in Directory.EnumerateFiles(caminho, "*.xml", SearchOption.AllDirectories))
    {
        try
        {
            DirecionarXML dir = new DirecionarXML();
            dir.direcionarXML(filePath, "", tipo);
            xmlimportsucess++;
        }
        catch (Exception ex)
        {
            registrarLog(ex.Message, filePath, "Err");
            xmlimporterror++;
        }
    }
}
Posted
Updated 3-Oct-19 3:59am

1 solution

Without your file system, there really isn't anything we can do to help.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Start by finding out where it's hanging and what it's' trying to do when it hangs - then you can start looking at "why". But until you know that, there isn't anything you can do either.
 
Share this answer
 
Comments
Member 11426986 3-Oct-19 10:09am    
I am using debugging. And that's why I came here. In debugging, after 1 minute and XML import, the system stops. It's as if the debug point is gone, but the performance manager continues, and the system becomes inaccessible, as if I was still debugging.
phil.o 3-Oct-19 10:17am    
At which point in your code does it start to hang?
You have put a breakpoint somewhere, and pressed F11 to execute line by line. On which line does the issue occur?
OriginalGriff 3-Oct-19 10:18am    
So start logging - every line if necessary - until you get information on exactly what it is doing just before it hangs.
We can't run your code under the same conditions - we don't have access to your XML files - so we can't help you find out even that basic information!
Member 11426986 3-Oct-19 11:06am    
The system imports all XML automatically, and works normally, until it reaches between 50 and 60 XML, approximately in the first minute after starting execution. If I import the rest of the manual, the problem is not displayed.
OriginalGriff 3-Oct-19 11:21am    
So start by looking at what your DirecionarXML.direcionarXML method is doing: is it kicking off Threads, or using memory, or what. It might be worth writing a "minimal" app to just load 100 files and see if that exhibits the same problems, or if i's a specific file that causes it.

I'm not trying to be awkward here: we don't have any of your data, or the rest of your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900