Timer and Function

Hello Everyone,

I am trying to trigger a function call every 1 second to see if the file in directory exists.
The code works fine when there is a file but didn’t seems to be working when there isn’t a file and gives

“RuntimeError: maximum recursion depth exceeded” and then
at org.python.core.Py.RuntimeError(Py.java:152)
at org.python.core.Py.JavaError(Py.java:490)
at org.python.core.PyTableCode.call(PyTableCode.java:168)
at org.python.core.PyBaseCode.call(PyBaseCode.java:120)
at org.python.core.PyFunction.call(PyFunction.java:307)
at org.python.pycode._pyx183.ReadFile$2(module:project.Reading_File:16)
at org.python.pycode._pyx183.call_function(module:project.Reading_File)
at org.python.core.PyTableCode.call(PyTableCode.java:165)" repeatedly.

The code I have is,
import time
from threading import Timer
import os

def timerFunc():
t = Timer(1000.0, ReadFile()).start()
system.gui.messageBox(“Waiting”)

def ReadFile():
contents = “C:\Users\kshah2\Desktop\file.asc”
if system.file.fileExists(contents):
File = system.file.readFileAsString(contents)
window = system.gui.getWindow(‘carlos’)
window.getRootContainer().getComponent(“Text Area”).text = File
else:
timerFunc()

Hmm. I’m not sure if this will truly solve your problem, but I believe how it is right now you are actually just calling ReadFile(), not passing it. So it hasn’t even made the timer yet, just running that function. Try

t = Timer(1000.0, ReadFile).start()

instead.

BTW, use three backticks, ```, above and below the code you paste and it will look much nicer.

Hi… Thank you so much.
Worked…
Yeah, sorry about that it’ just I am new in Forum and didn’t quite knew how can I do that.

Thanks again.

1 Like