-4

I am working on an C# application which creates a text file with some data in it, saves it in a folder, sends it to a list of email addresses and deletes the file from that location but when I call the File.Delete() it throws me an exception saying file cannot be accessed as it is being used by another process. That is because the file is being used by the email service and trying to delete so, its an obvious exception but when I tried to a Delay between two function calls, it still threw me an exception

  _dailyBargainReport.sendRejectionReport(servername, fromAddress, password, sub, bodyofmail, rejectionReportPath);

             Task.Delay(20000);
            File.Delete(rejectionReportPath);
DoIt
  • 3,270
  • 9
  • 51
  • 103
  • 2
    I'm going to assume that `sendRejectionReport` has no using statements within. Everything that implements `IDisposable` needs to be wrapped in a using statement. – ChaosPandion Jul 25 '14 at 13:29
  • What does `.sendRejectionReport` look like? Is it asynchronous? – Matt Burland Jul 25 '14 at 13:29
  • please show the code that you are using to create the file – faby Jul 25 '14 at 13:30
  • @ChaosPandion No it doesn't – DoIt Jul 25 '14 at 13:30
  • 1
    As a side-note, your `Task.Delay` doesn't help because you're not awaiting the task. Either do this `await Task.Delay(2000)` or simply block the thread `Thread.Sleep(2000)` – dcastro Jul 25 '14 at 13:30
  • @dcastro. I tried using Thread.Sleep, it didnt work either – DoIt Jul 25 '14 at 13:31
  • 1
    What is this omnious sendRejectionReport method? Does the deletion of the file still fails if you don't call that method (i.e., is the problem *really* related to calling sendRejectionReport, or is there perhaps something else going on)? –  Jul 25 '14 at 13:45
  • The real solution is to tell us what `sendRejectionReport` does and figuring out a way to signal that it's done with the file. Failing that, you could slap a bandaid on it with something like `Task.Delay(20000).ContinueWith(() => File.Delete(rejectionReportPath));` – Matt Burland Jul 25 '14 at 14:05

2 Answers2

1

I think that your problem is that you aren't calling the Dispose method on FileStream

using (FileStream f = File.Open("example.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
    //do your operations
}
File.Delete(rejectionReportPath);

using statment always call Dispose so is equivalent to

try{
   FileStream f = File.Open("example.txt", FileMode.Open, FileAccess.Read, FileShare.None);
}
finally{
   ((IDisposable)f).Dispose();
}
//delete file here

update

try in this way to wait the function

Task.Factory.StartNew(() =>
    {
        _dailyBargainReport.sendRejectionReport(servername, fromAddress, password, sub, bodyofmail, rejectionReportPath);
    })
    .ContinueWith(() =>
    {
        File.Delete(rejectionReportPath);
    }).Wait();

In this way you are sure that Delete function is called after the end of sendRejectionReport.
Remembar to call Dispose inside sendRejectionReport function

faby
  • 7,394
  • 3
  • 27
  • 44
0

When creating the file, you could create it with a flag to be deleted when the process closes. See: https://stackoverflow.com/a/400433/3846861

Community
  • 1
  • 1
kzagar
  • 355
  • 2
  • 4