Android Question Create txt file and continue only when done [solved]

trepdas

Active Member
Licensed User
Hello good people and the mighty binary god,

I created a txt file and I need to continue only after this is done.

B4X:
Writer.Initialize(File.OpenOutput(File.DirInternal, "index.html" , False))
    Writer.WriteLine (FinalHtml)
    Writer.Close

Log ("index file created.")

' wait for confirmation that this file exists before continue'



What would be the best method with WaitFor ?
 

Mahares

Expert
Licensed User
Longtime User
What would be the best method with WaitFor
One way is:
B4X:
wait for (WriteMe) Complete (Res As Object)
    Log ("index file created.")
B4X:
Sub  WriteMe As ResumableSub
    Dim writer As TextWriter  
    writer.Initialize(File.OpenOutput(File.DirInternal, "index.html" , False))
    writer.WriteLine (FinalHtml)
    writer.Close
    Return Null    
End Sub
Note there is also File.WriteString as an option. Textwriter has become less popular
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
You can't use Wait For in this situation as the Writer methods are synchronous so there is nothing to wait for and the file should be available immediately after the Close. If you are not having a specific problem then your code should just work. Are you having problems?

Having said that I have seen very occasionally in one specific program handling big image files that there was a delay after Close before the file was available. I think down deep in Android there might be some asynchronism in file handling or it might require a return to the message loop to complete the process under some circumstances. Assuming there is no error during writing then just looping until a File.Exists is True is probably guarantee enough.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Hello good people and the mighty binary god,

I created a txt file and I need to continue only after this is done.

B4X:
Writer.Initialize(File.OpenOutput(File.DirInternal, "index.html" , False))
    Writer.WriteLine (FinalHtml)
    Writer.Close

Log ("index file created.")

' wait for confirmation that this file exists before continue'



What would be the best method with WaitFor ?
I don't understand why do you need to wait for?
What are you going to wait for?
You have the steps: initialize writer -> then write line -> then close the the writer. The process is completed after that - if the file can be opened/created. What do yo expect "waitfor" will do?
If you want to check if the file exist after you wrote to it, do the check: If File.Exists(.....) then
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can't use Wait For in this situation as the Writer methods are synchronous
I tested the code I put in post#2 with Wait For and it works. The file is saved and no errors were reported. Whether he needs it or not , probably not, but I refrain from commenting on that decision.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
I tested the code I put in post#2 with Wait For and it works. The file is saved and no errors were reported. Whether he needs it or not , probably not, but I refrain from commenting on that decision.
Of course it works. Because it doesn't do anything different from your code in post #1!

So, did you think what the code in post #2 does? If you did, you would see that it is not waiting for anything! After "writer.Close" it returns. There is no indication that the writing process is complete or failed.
After "writer.WriteLine (FinalHtml)", whether the line was written or not, you close the writer "writer.Close". If the file couldn't be opened or created you it doesn't tell you. So waiting for something that doesn't give you any indication of error or completion doesn't make sense.
 
Upvote 0

trepdas

Active Member
Licensed User
Thank you so very much good people.
I am amazed each and every time from the kind people around here who are there to really help. Erel, you have build a wonderful community.

as for the topic :
right after the file creation , I send it to upload with ftp to a ftp server.

however, in the log I got "file not found"
I don't know why the binary god is so cruel to me, it's got to be something bad I did in the past.
maybe it's because I'm using an old slow phone as the emulator?
anyway, I tried "sleep(500)" in between and it worked fine! this is why I wanted to delay a bit or to loop until I'm positive that the file created and exists.

I've been told by smart people in the past to avoid the sleep command if I can.
so the If File.Exists(.....) then worked just fine,

THANK YOU !
šŸ™ šŸ’œ
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've been told by smart people in the past to avoid the sleep command if I can.
That was probably the old synchronous Sleep which hung the main thread. The new Sleep, which is effectively a Wait For, is fine and is very useful to let the message loop run when you want it to without apparently interrupting the flow of your code.. As I indicated I did experience a similar problem in immediately using a new file and in fact solved it with Sleep(0) if I remember correctly - but failed to mention this in my reply above.
 
Upvote 0
Top