0

I have a print form which does the printing jobs. When I close the print form without printing I click Close button

Close button has

    private void Close_Click(object sender, EventArgs e)
    {
        PublicVariables.PrintData = -1;
        PublicVariables.PrintStat = false;
        ppc.Document = null;
        ppc.Dispose();
        streamToRead.Close();
        this.Hide();
    }

But each time I create a text file to print I delete the old.

Delete method :

    public static bool DeleteData()
    {
        bool result=true;
        string pattern = "data??.txt";
        string appPath = Path.GetDirectoryName(Application.ExecutablePath);
        var matches = Directory.GetFiles(appPath, pattern);
        foreach (string file in Directory.GetFiles(appPath).Intersect(matches))
        {
            try
            {
                File.Delete(file);
                result =true;
            }
            catch (IOException e1)
            {
                MessageBox.Show(e1.ToString());
                return false;
            }
        }
        return result;
    }

But if an IOException occurs can't delete any file. However form load of all threads I have DeleteData() and this deletes without problem the text data.

Is there a way to delete this text file within in the thread where it's created ?

For those who will advise me to make an hidden form which will delete data. I did it I got always an IOexception error. After few IOexception errors all data??.txt files are erased but it happens randomly.

Here below two procedures which create data??.txt

http://www.turcguide.com/stack/procedures.txt

Here is the CreateDataFile(string fName) and GetNewfName(string oldName) procedures link:

http://turcguide.com/stack/createdatafile.txt

Ismail Gunes
  • 548
  • 1
  • 9
  • 24
  • What is the exact error message? Is the document opened at the time when you try to delete? – grek40 Dec 31 '15 at 14:12
  • @grek40 File is busy with vshost. I have the same method exists in different thread they work. I looked if I did not properly closed the file which created the data??.txt (data00.txt, data01.txt so long till data99.txt). It seems all they are closed after the creation of data??.txt As show in my method after disposing printpreviewcontroller I close thre stream reader. The deletion comes just before I create a new data. – Ismail Gunes Dec 31 '15 at 15:09
  • Does the same problem occur when you run the code outside visual studio debugger? (See http://stackoverflow.com/questions/774187/what-is-the-purpose-of-vshost-exe-file) – grek40 Jan 01 '16 at 11:28
  • I stopped vshost project and even I deleted vshost.exe at it's manifest, still I am getting same warning file is busy. The data??.txt file since I get this warning I enclosed by using statement the streamwriter method. When I read for displaying on printpreviewcontroller streamreader is closed too after reading. What I know a file is busy if it's not closed but after many verification I am sure it's closed – Ismail Gunes Jan 01 '16 at 12:25
  • The linked textfile does not contain the code where you create the files, which would be somewhere in `GeneralMethod.CreateDataFile` – grek40 Jan 01 '16 at 14:10
  • CreateDataFile("data00.txt") is a procedure that gives me first disponible data??.txt. Because for some reason I have more then one page so it creates data01.text data02.txt so long. I edited for giving the link for those procedures. Other part of my program uses the same public procedure and gives no error. – Ismail Gunes Jan 01 '16 at 16:43
  • Where is this file? If it's in Program Files\ Windows is going to object to that delete command. Other than that I would say you have a resource not being properly disposed of. – Loren Pechtel Jan 01 '16 at 19:23
  • @LorenPechtel the file is created in the debug menu. As shown in my CreateDataFile, data??.txt is created in the application folder. So compiled version create and looks for this file in application path. So there is no problem. How ever when I try 5-6 or more times DeleteData() procedure deletes all created files. But I don't still know why and what is bloking this file as they properly closed after creation and after read – Ismail Gunes Jan 01 '16 at 22:19
  • Yeah, I can see it's created in the application's folder. If the application is installed in Program Files\ like applications normally are Windows will not allow the delete--Program Files\ is read-only to . – Loren Pechtel Jan 01 '16 at 22:27
  • @LorenPechtel Ok. All the sources are in another drive then the system drive. As I told in my preview message after few attemps this file is erased. – Ismail Gunes Jan 01 '16 at 22:34
  • Then the file is still being held by a closed but not disposed object. – Loren Pechtel Jan 02 '16 at 01:56
  • @LorenPechtel You're right but I don't see this object. Because. This file is read with streamreader and it's closed. It's displayed on the screen by printpreviewcontroller it's disposed printdocument also is disposed and set to null. So I begun to suspect a bug in .net 3.5 (as I use vs 2008) – Ismail Gunes Jan 02 '16 at 11:05
  • @LorenPechtel As I display data??.txt on printpreviewcontroller it should act as printer so the file is held for a given time on printer. Because when I try to delete data??.txt 4-5 times it's deleted. If this file still remains opened or blocked it will never be deleted. – Ismail Gunes Jan 02 '16 at 12:25

1 Answers1

0

The printpreviewcontroler should block this file as when we cancel a printing document on printer it needs time.

After closing and disposing the printpreviewcontroller form, when I try to delete 4-5 attempts later the file is deleted.

My point of view it comes from printpreviewcontroller.

Ismail Gunes
  • 548
  • 1
  • 9
  • 24
  • My point of view: you have *some* place where you do not close/dispose correctly and it is probably not part of the code that you provided in the question. – grek40 Jan 02 '16 at 13:27
  • @grek40 there is no other place where I treat this file, and when I try to delete this file after few attempt it's deleted as we cancel a printing job which need a little laps of time before it's deleted. If it's really hold and not closed properly it will never be deleted. I changed my code which fills printpreviewcontroller, and created data??.txt and showed it on an listview. It's deleted just after closing the file. – Ismail Gunes Jan 02 '16 at 14:29
  • 1
    Well, if you don't close it, the native file handle is closed when the managed file handle is finalized, which happens at any unspecified time after your last reference is gone. So you can delete the file after the garbage collector finished its work. You can check it by calling `System.GC.Collect()` before trying to delete files. If that solves your problem, then you have not closed file in all locations. – grek40 Jan 02 '16 at 15:05
  • @grek40 Finally I found my fault. I forgot a line streamtoread = StreamReader(fName,true) in form_load and it was never used. But it was blocking streamtoread for a while and after as it was never used. GC was freeing this As streamtoread is a public variable this was causing an exception. – Ismail Gunes Jan 26 '16 at 11:59