0

I have a function whitch creates .xml files with some data inside. Everytime firstly it deletes the old file. Everything works fine except it sometimes gets frozen and the file itself becomes locked. It's size is 0 kb and program can't even delete it. And I have to kill the process, delete the file by myself and then run the program again. Is it possible to kill all the processes of the same program before the begining of a new one? Or at least put some timer on it to make sure it turns off automatically after some time passes by?

Need some ideas, thanks.

fHandle = f_cFile("D:\Data\new_eur\Saskaitos.xml")
if fHandle < 0
**=messagebox("Can't create file!",16,"!!!")
=STRTOFILE("Can't create XML file" + CHR(13) + CHR(10), "d:\Log.txt", 1)
quit
ENDIF

** Header
if fputs(fHandle, '<?xml version="1.0" encoding="windows-1257"?>') < 0
=fclose(fHandle)
=STRTOFILE("Can't write to XML file" + CHR(13) + CHR(10), "d:\Log.txt", 1)
quit
endif

=STRTOFILE(TTOC(dateTIME()) + ": " + "XML ok"+CHR(13)+CHR(10), "d:\LogData.txt", 1)
=fputs(fHandle, "<Accounts>")

enteris = CHR(13)
DO WHILE NOT EOF()
=fputs(fHandle, "<Detali>")
=fputs(fHandle, "<Snr><![CDATA[" + ALLTRIM(Data.dok_nr) + "]]></Snr>" + enteris)
=fputs(fHandle, "<Code_ks><![CDATA[" + ALLTRIM(Data.Code_ks) + "]]></Code_ks>" + enteris)
=fputs(fHandle, "<Sdata>" + ALLTRIM(Data.dok_data) + "</Sdata>" + enteris)
=fputs(fHandle, "<Term>" + ALLTRIM(Duomenys.Terminas) + "</Term>" + enteris)
=fputs(fHandle, "<Manager><![CDATA[" + ALLTRIM(Data.Code_ms) + "]]></Manager>" + enteris)
IF Data.val_poz = 0
    =fputs(fHandle, "<MokSuma>" + ALLTRIM(STR(Duomenys.SumaMok, 12, 2)) + "</MokSuma>" + enteris)
    =fputs(fHandle, "<ApSuma>" + ALLTRIM(STR(Duomenys.ApSuma, 12, 2)) + "</ApSuma>" + enteris)
 ELSE
    =fputs(fHandle, "<MokSuma>" + ALLTRIM(STR(Duomenys.SumaVal, 12, 2)) + "</MokSuma>" + enteris)
    =fputs(fHandle, "<ApSuma>" + ALLTRIM(STR(Duomenys.ApVal, 12, 2)) + "</ApSuma>" + enteris)
ENDIF 
=fputs(fHandle, "</Detali>")    
skip
ENDDO
fputs(fHandle, "</Accounts>")
=fclose(fHandle)

Now this is the code which puts xml data into the file. At some point it freezes and the next time program starts it is still using the same file.

Function f_cFile:

FUNCTION f_cFile
PARAMETERS fName
fHandle = fcreate(fName)
IF fHandle < 0
    IF FILE(fName,1)
        DELETE FILE fName
        IF FILE(fName,1)
            =STRTOFILE("Can't delete old file: " + fName + CHR(13) + CHR(10), " d:\Log.txt", 1)
        ELSE    
            fHandle = fcreate(fName)        
        ENDIF   
    ENDIF
ENDIF
RETURN fHandle
ENDFUNC
The50
  • 1,096
  • 2
  • 23
  • 47
  • Can you paste the code you are doing to create the XML file and trying to delete? Is it low level or simple with CursorToXML() or using XMLAdapter class? – DRapp Jan 10 '17 at 13:31
  • I edited the first post, added some code parts, maybe it is more clear now. – The50 Jan 10 '17 at 13:50
  • Your code seems to be doing it the fastest way possible in VFP. I think you should give info about the files and how they are related. One note though, instead of fHandle, use m.fHandle. You don't need 'enteris' variable, Fputs() automatically adds CR (chr(13)) and also LF (chr(10)). It is strange that this code hangs (reminds me the data is on a remote machine and network is very slow - then I wouldn't trust using File() at all). – Cetin Basoz Jan 11 '17 at 12:02

1 Answers1

1

Wow... many other options to simplify your locking issue. First without changing your stuff too much is to build via strings... Make a variable and keep appending to it until you are done, then write it with a single write command such as..

enteris = CHR(13)
myXML = '<?xml version="1.0" encoding="windows-1257"?>' + enteris;
        + "<Accounts>"

SCAN 
    myXML   = myXML + "<Detali>";
            + "<Snr><![CDATA[" + ALLTRIM(Data.dok_nr) + "]]></Snr>" + enteris;
            + "<Code_ks><![CDATA[" + ALLTRIM(Data.Code_ks) + "]]></Code_ks>" + enteris;
            + "<Sdata>" + ALLTRIM(Data.dok_data) + "</Sdata>" + enteris;
            + "<Term>" + ALLTRIM(Duomenys.Terminas) + "</Term>" + enteris;
            + "<Manager><![CDATA[" + ALLTRIM(Data.Code_ms) + "]]></Manager>" + enteris

    IF Data.val_poz = 0
        myXML = myXML + "<MokSuma>" + ALLTRIM(STR(Duomenys.SumaMok, 12, 2)) + "</MokSuma>" + enteris;
                + "<ApSuma>" + ALLTRIM(STR(Duomenys.ApSuma, 12, 2)) + "</ApSuma>" + enteris
    ELSE
        myXML = myXML + "<MokSuma>" + ALLTRIM(STR(Duomenys.SumaVal, 12, 2)) + "</MokSuma>" + enteris;
                + "<ApSuma>" + ALLTRIM(STR(Duomenys.ApVal, 12, 2)) + "</ApSuma>" + enteris
    ENDIF 
    myXML = myXML + "</Detali>";
ENDSCAN 

myXML = myXML + "</Accounts>"

if StrToFile( myXML, "D:\Data\new_eur\Saskaitos.xml" ) = 0
    =STRTOFILE("Can't create XML file" + CHR(13) + CHR(10), "d:\Log.txt", 1)
endif 

Done... Let VFP handle the low level open and close of the writing.

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • I wouldn't suggest doing it this way at all. String operations in VFP is slow, and adding to a string over and over would only help consuming more memory. This approach would likely to fail much before than an Fputs() approach. – Cetin Basoz Jan 11 '17 at 12:04
  • Any other ideas then? – The50 Jan 11 '17 at 13:05
  • @The50, I already added comment under your post. What you are doing seems to be the fastest way of doing it already. For creating the xml in a different way, you could use XMLAdapter class (cursortoxml wouldn't help as is) but if it is hanging using FPuts() then you should first understand why it does hang in the first place. – Cetin Basoz Jan 11 '17 at 14:26