In my app I'm trying to write to a file in a seperate thread, but somehow I can't get threading to work correctly. Basically what I'm doing is:
for i in range(2):
appuifw.app.body.set(u"Running: ", i)
When I use this method the UI freezes. I read the piece about active objects in the manual, but that doesn't seem to be what I want. Any suggestions or simple example how to get threads working?
Python for Series 60 only allows you to access an open file from the thread in which you opened it. Could you perhaps open the file in the worker thread?
For logging to a file from several threads, I have found the simplest approach to be to open the file in append mode before each write and close it afterwards. That can be bulletproofed by wrappinng the whole thing in a mutex.
But sorry, I didn't read your code snippet when I wrote the reply above. The code seems to have a number of problems, and I'm not sure how it relates to your question. You'll need to clarify.
What I want to accomplish is a thread running in the background that writes something to a text file every few seconds. How do you do that without freezing the UI? The skeleton example that comes with the installation is not a useful example for that.
I get the feeling that there's something wrong with the way I create new threads since they freeze the UI.
Having a thread write something to a text file occasionally should not be a problem. The problem may lie elsewhere. Could you please post the code in question? The snippet you included in your original message does not write to a file and doesn't actually run at all. -Ken
Sorry, here's the code, which seems to be working:
self.lock = e32.Ao_lock()
appuifw.app.exit_key_handler = self.abort
appuifw.app.body = appuifw.Text()
appuifw.app.menu = [(u"Exit", self.abort)]
# enter loop to prevent finishing the app
self.keepRunning = True
# update UI if necessary
appuifw.app.body.set(u"App running: " + time.strftime("%H:%M:%S\n"))
self.keepRunning = True
self.output = open("c:\\tmp\\output.txt", "a")
self.keepRunning = False
appuifw.app.exit_key_handler = None
if __name__ == "__main__":
I'm not sure how elegant this is.... :)
One thing I forgot was putting the main application in a loop. I'm also wondering whether UI changes will be processed and visible in this way.
...I'm also wondering whether UI changes will be processed and visible in this way.[/QUOTE] Sure, UI changes will be processed and made visible as long as you are blocked on wait(). The tricky part is making the changes to the UI state, which is only safe to access in the main thread. Feasible places to do it in the particular style you are using are event handlers and your refresh method. In the latter case, you have to signal the lock (which you can do from any thread) whenever you want the refresh method to execute. -Ken
Is it necessary to use e32.ao_sleep(5) instead of time.sleep(5) ?
No, I don't think so. It's just that I tried e32.ao_sleep() instead of time.sleep() because I couldn't get it to work. But that didn't solve the problem. Now that I solved it I found out that time.sleep() works just as well.
I'm not exactly sure what the difference between both is. Anybody?
Re: Thread example
I got this error while Installing the jar in pda "Fatal error: KErrCorrupt"
i need the Solution immediately.............
please send now itself.. thanks..