Jump to content

Thread-safety considerations in MainModule


Tim

Recommended Posts

Hi,

 

In my application, I store information the user enters on pages of a wizard in fields of TUniMainModule; when the wizard is finished, this information gets submitted to our server via webservice calls. I am just wondering to what extent I need to worry about thread safety when reading or writing fields of TUniMainModule? I only read or write fields of TUniMainModule in event handlers of controls on the forms and frames that make up the wizard. This includes OnClick events of TUniButtons and TUniSpeedButtons, OnTimer events of TUniTimers, OnCreate and OnShow events of TUniFrames and TUniForms, OnChange events of TUniComboboxes, TUniEdits and TUniCheckboxes, OnAfterLoad events of TUniDBGrids, and OnAjax events of TUniHTMLFrames and TUniForms. Indeed, calling UniMainModule() from any context other than an event handler (e.g. from a the Execute method of a TThread descendant that one has created) raises an exception - i assume this is by design.

 

So I guess my question is: Is there any possibility of two event handlers being triggered simulteneously? For example, if the user double-clicks a button, or if the OnTimer event of a TUniTimer happens to fire at exactly the same time as the user clicks on some other control? Or does UniGui prevent this somehow? If not, then i presumably have to protect all access to TUniMainModule fields with a critical section or similar. How do other UniGui developers handle this in their projects? I would be grateful for any insights.

 

Thanks,

Tim

Link to comment
Share on other sites

  • Administrators

Hi,

 

In my application, I store information the user enters on pages of a wizard in fields of TUniMainModule; when the wizard is finished, this information gets submitted to our server via webservice calls. I am just wondering to what extent I need to worry about thread safety when reading or writing fields of TUniMainModule? I only read or write fields of TUniMainModule in event handlers of controls on the forms and frames that make up the wizard. This includes OnClick events of TUniButtons and TUniSpeedButtons, OnTimer events of TUniTimers, OnCreate and OnShow events of TUniFrames and TUniForms, OnChange events of TUniComboboxes, TUniEdits and TUniCheckboxes, OnAfterLoad events of TUniDBGrids, and OnAjax events of TUniHTMLFrames and TUniForms.

 

In above scenario you are safe. Framework guarantees that only one event is running at a time for any particular session.

 

Indeed, calling UniMainModule() from any context other than an event handler (e.g. from a the Execute method of a TThread descendant that one has created) raises an exception - i assume this is by design.

 

Yes, objects UniMainModule, UniSession and UniApplication are valid only if you access them within a uniGUI event. Trying to access them from other threads returns nil.

 

So I guess my question is: Is there any possibility of two event handlers being triggered simulteneously?

 

No.

 

For example, if the user double-clicks a button, or if the OnTimer event of a TUniTimer happens to fire at exactly the same time as the user clicks on some other control?

 

No. All events for same session are queued and executed in a proper order.

 

Or does UniGui prevent this somehow?

 

Yes, uniGUI avoids it as explained above.

 

If not, then i presumably have to protect all access to TUniMainModule fields with a critical section or similar. How do other UniGui developers handle this in their projects? I would be grateful for any insights.

 

There is no need to use Critical sections. All these are handled internally.

 

Thanks,

Tim

Link to comment
Share on other sites

  • Administrators

Hi,

 

In my application, I store information the user enters on pages of a wizard in fields of TUniMainModule; when the wizard is finished, this information gets submitted to our server via webservice calls.

 

I assume web service calls are also executed from a regular uniGUI event. Correct?

 

In uniGUI there are also mechanisms to run tasks in different threads. Please see below demos:

 

ThreadTimer

ThreadTimer-1

ThreadTimer-2

Link to comment
Share on other sites

Hi Farshad,

 

Thanks for the reply. Good to know that i don't have to put critical sections all over the place :)

 

Actually the webservice calls are not executed from a regular UniGui event, due to the need to display progress to the user. When the user clicks the button to complete the wizard, I create an independent copy of all the necessary data (which has been stored up to that point in TUniMainModule) and pass that to the constructor of a TThread descendant; the Execute method of the TThread descendant performs the webservice calls and writes its progress to a global variable (protected by a critical section). The OnTimer event of a TUniTimer on TMainForm reads the value of this global variable (protected by a critical section),

and updates the Caption of a TUniLabel accordingly. This implementation seems to work well and is based on tips i received in the following thread:

 

http://forums.unigui.com/index.php?/topic/4939-displaying-progress-during-time-consuming-operations

 

Thanks for the pointer to those sample projects. I didn't know about TUniThreadTimer and TUniGUISession.LockSession. I will consider using these in future projects.

 

Regards,

Tim

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...