#1 2019-02-07 16:18:27

mrluis
Member
From: Florida
Registered: 2017-08-20
Posts: 24

Sync to main thread

Hi guys,


I'm doing this small project where the customer can execute JS an application. The application initializes the engine and executes it mostly without problem. The scripting engine does its job, I think the problem is my implementation to extend it.

I created a few classes that provide access to the database from the script... that works perfect, however, there are 2 "utilities" classes that seem to randomly freeze the application half way thru the script. Those classes are done to interact with the main form... And I believe that's where my bug lies.... It behaves like when you access form's components from a thread without synchronizing it. On a threat is easy to fix, because the Sync procedure of TThreat, however here I'm not sure how to sync with the main thread!


Any suggestion?


Luis

Offline

#2 2019-02-07 19:29:47

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Sync to main thread

I haven't used SyNode, so cannot help in that area. However I do have experience with VCL safe multithreading, those utility classes are they written in Delphi?
Do you need to retrieve some data from MainForm or just send commands to it? If it's only to send commands, safe and easy way is to PostMessage custom message to it and then write message handler in the main form, it will execute in the context of the mainthread so no problem will arise. If you need to get some information from main form, it depends on what that is, in some cases it could be safe to read property directly, however a safe and proper way is to ensure that reading is done in main thread context. So from subthread you can send command to mainForm (PostMessage is fine as well), set up some event (TEvent), and wait for main form to process command, return result and signal event. Basically all Sync methods of threads are doing something like that, executing method in the mainthread context and waiting for it to finish. Why can't you use TThread.Synchronize?

Offline

#3 2019-02-07 19:39:44

mrluis
Member
From: Florida
Registered: 2017-08-20
Posts: 24

Re: Sync to main thread

Post Message might do the trick (since I'm only adding info to a memo component... emulating the output of console.log).


I can't use Thread.Synchronize because I'm actually "tapping" out of an event... that event seems to be running in a thread other than main. Is not like me creating a TThread instance...


Let me give a try to Post Message!

Offline

#4 2019-02-10 18:49:04

mrluis
Member
From: Florida
Registered: 2017-08-20
Posts: 24

Re: Sync to main thread

First... Thanks, igors233 for your suggestion!

Second... my bad... The problem was not multi-thread related...  It happens that the database I was updating was leaving the connection on 'Suspense' because I tried to alter a table after having a cursor opening the same table... This pretty much locks itself!   no good!

Third... something weird I notice is that if I call evaluate within a thread (not been the main thread) it runs, however any loop that have more than 8 repetitions will generate an AV.  It could be another bug on my code... It would not surprise me!  but it reflects on the engine itself, and I can not even stop the debugger on the code generating the AV...  Weird, but it does not happens if the eval runs on the main thread, so no harm, no foul...


Thanks
Luis

Offline

Board footer

Powered by FluxBB