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:
int cte = 0;
if (Directory.Exists(caminhocte))
{
foreach (string filePath in Directory.EnumerateFiles(caminhocte, "*.xml", SearchOption.AllDirectories))
{
cte++;
}
}
int nfe = 0;
if (Directory.Exists(caminhonfe))
{
foreach (string filePath in Directory.EnumerateFiles(caminhonfe, "*.xml", SearchOption.AllDirectories))
{
nfe++;
}
}
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 (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;
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);
registrarLog(messagesucess, "", "Reg");
GC.Collect();
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++;
}
}
}