End a process without closing the executable

Asked

Viewed 1,001 times

4

I’m developing an integrated system with a TEF. Every time a credit card transaction is performed the TEF creates a file with a specified name in a specific directory, my application reads this file and performs the download and prints the receipt.
Happens at random times the system gives me an exception:

System.IO.Ioexception: Process cannot access file 'C: XXXXXX xxxx.xxx' because it is being used by another process.

I’m using the API Restart Manager to check if the file is already in use, but when identifying that yes, as who is using the file is the application itself, I can not do processo.Kill() because it will close my own application.

Always after I use the file I’m putting arquivo.close().
I don’t understand why you’re still running.

try {
  StreamReader sr = new StreamReader(e.FullPath);
  line = sr.ReadLine();
  while (line != null)
  {
    //Código da para armazenas as informações importantes em variáveis
    line = sr.ReadLine();
  }
  sr.Close();
} catch (Exception a) {
  // mensagem da exceção
}

I can see which process is using the same file I’m trying to read, I did based on this answer: How to know if the file is being used by another process before trying to read

I need to end this process and/or close the file that was opened by the other process, without closing the application

  • 1

    It may be open and being accessed by another process.

  • 1

    can post part of the code to analyze ?

  • 1

    Yes it certainly is, but the other process is of my own application, which somehow did not finish, how can I force this process to be finalized without closing my application?

  • 1

    @Jonathan Use the button Edit below your question to include new information /codes to improve your question. Comments are not a good place for this. By the way, welcome to SOPT!

  • Thanks for the tip

  • I also recommend: [ASK] and [TOUR]. These pages contain some recommendations to ask good questions.

  • Ultilize the Finally. Try{ tries,... } catch (Exception e) { //catches the error } Finally { apos everything and before the sr.Close Return(); }

  • I changed sr.close() to just before Return and includes Finally, I will wait because the error occurs after a large number of transactions, if it works I will let you know here.

  • It wouldn’t be the case to write your app as a service?

  • Unfortunately even changing the close() to before the place Return and inserting the Finally, the problem still persists.

  • @Jonathan You create this, archive and soon after read? will it not be that it is not getting open in the creation itself?

  • who creates the file and a second application, which controls the TEF, I do a read in the directory, and I wait for the creation of the file when the file is created with the name I am waiting for, I read it

  • @Your application Jonathan is windows form or web, or neither of the two ? (:

Show 8 more comments

1 answer

1


You can try reading the file using a Filestream and Streamreader by specifying the accesses instead of the default constructor. This might solve:

    public string Executa(string caminho)
    {
        var conteudo = new StringBuilder();
        using (var fileStream = new FileStream(caminho, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (var streamReader = new StreamReader(fileStream, Encoding.Default))
            {
                var linha = streamReader.ReadLine();
                while (!string.IsNullOrWhiteSpace(linha))
                {
                    //Código da para armazenas as informações importantes em variáveis
                    conteudo.AppendLine(linha);

                    linha = streamReader.ReadLine();
                }

                streamReader.Close();
            }
        }

        return conteudo.ToString();
    }

Try it on and see if it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.