Openfile() wasting time.

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Openfile() wasting time.

Post by matalog »

I'm using a strange method of sending data, over WIFI to a WIFI SD Card, and trying to stream to it. It seems to have the biggest problem using Openfile() on the WIFI card, it takes 1-3 seconds to complete, and in my program this stops the whole program running as it waits.

I would not close any files if the data was updated on receiving device, but it isn't updated until I Closefile().

How could I program the Closefile() and a new Openfile() to run parallel to the running of the program, and not stall my program?

Another option would be to open many files at the start of the program, and then close them as it runs (Closefile() ) doesn't seem to stall the program as much.

Any ideas?
Fred
Administrator
Administrator
Posts: 18352
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Openfile() wasting time.

Post by Fred »

You need to use threads
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Re: Openfile() wasting time.

Post by matalog »

Fred wrote: Mon Dec 04, 2023 3:07 pm You need to use threads
Yes, I do, I don't know much about using them, but seem to have something almost working. Thanks.
BarryG
Addict
Addict
Posts: 4225
Joined: Thu Apr 18, 2019 8:17 am

Re: Openfile() wasting time.

Post by BarryG »

This should explain it better. It simulates writing a slow file to disk while not slowing down the main loop (the counter never stops).

Code: Select all

CompilerIf #PB_Compiler_Thread=0
  MessageRequester("Compile Error","Thread-safety is not enabled!",#PB_MessageRequester_Error)
  End
CompilerEndIf

Procedure SimulateSlowFile(null=0)
  DisableGadget(0,#True)
  SetGadgetText(2,"Writing file...")
  f$=GetTemporaryDirectory()+"slow-file-test.txt"
  f=OpenFile(#PB_Any,f$)
  If f
    Delay(5000) ; 5-second delay to simulate a slow write.
    CloseFile(f)
    DeleteFile(f$) ; Don't leave it behind after this test. ;)
  EndIf
  SetGadgetText(2,"")
  DisableGadget(0,#False)
EndProcedure

OpenWindow(0,200,200,300,100,"Test",#PB_Window_SystemMenu)
ButtonGadget(0,10,10,280,25,"Click to write file to disk")
TextGadget(1,10,40,100,20,"")
TextGadget(2,10,60,100,20,"")
AddWindowTimer(0,0,50)

Repeat
  ev=WaitWindowEvent()
  If ev=#PB_Event_Timer
    n+1
    SetGadgetText(1,Str(n))
  ElseIf ev=#PB_Event_Gadget
    If EventGadget()=0
      writing=CreateThread(@SimulateSlowFile(),0)
    EndIf
  EndIf
Until ev=#PB_Event_CloseWindow And IsThread(writing)=0
User avatar
mk-soft
Always Here
Always Here
Posts: 6329
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Openfile() wasting time.

Post by mk-soft »

matalog wrote: Tue Dec 05, 2023 12:05 am
Fred wrote: Mon Dec 04, 2023 3:07 pm You need to use threads
Yes, I do, I don't know much about using them, but seem to have something almost working. Thanks.
Show Mini Thread Control with many examples ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Re: Openfile() wasting time.

Post by matalog »

Thanks for that :-).
Post Reply