My thread cannot delete PDF files. When I debug this solution, the file is deleting. In service mode, it is not deleting.
This is my sample code:
Thread Worker = new Thread(new ThreadStart(Start)); //Main Function
Worker.Start();
public void Start()
{
string text=PDFToText(@"C:\1.pdf");
File.Delete(@"C:\1.pdf");
}
private string PDFToText(string filename) // My PDF reader function
{
PDDocument doc = PDDocument.load(filename);
PDFTextStripper stripper = new PDFTextStripper();
string text = stripper.getText(doc);
doc.close();
return text;
}
Where is the problem?
What is possibly happening is that something (probably the API you are using) has a lock on the file, preventing the delete.
When you do this in debug and step through, because you're stepping through it at a "slow" pace, the lock has been released, and the file is successfully deleted.
You should put the deletion code in a retry loop with an exception handler, which sleeps for a second or so and then tries again, giving OS a chance to relinquish the lock.
Another thing to consider is that, assuming you are running this as a service when in production, the service is running under a different user-account than when you debug it through visual studio (assuming you are not attaching to the service - it's hard to tell here). Check that the account the service is running under has permissions to delete the file in the first place.