CapableObjects forum



Accessing EcoSpaces safely from Timer.Elapsed

Posted By lbernas 2014-01-23 08:45:15
Add to Favorites0
Author Message
lbernas
 Posted 2014-01-23 08:45:15
Supreme Being

Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)

Group: Awaiting Activation
Last Active: 2014-03-11 13:18:59
Posts: 21, Visits: 264
Hi,

Has anyone here had the need to access EcoSpaces from a timer (either System.Timers.Timer or System.Threading.Timer, not the WinForms one)? As the Elapsed event handler will execute in a separate thread, as far as I understand, this is not safe if the EcoSpace can be accessed from its own thread at the same time.


In our particular case, we want the timer to invalidate time dependent derived members in EcoSpaces running in an ASP.NET application, or in a web service. Setting a persistent member, subscribed to in those derivations, in a dedicated EcoSpace (only accessed by the timer) would be safe, but it doesn't feel like the most elegant solution. Any better suggestions? Smile

Regards,
Linus Bernas
HansKarlsen
 Posted 2014-01-28 18:07:23
Supreme Being

Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)Supreme Being - (102 786 reputation)

Group: Administrators
Last Active: 2016-05-11 20:56:01
Posts: 608, Visits: 3 230
Since the ecospace is not not thread safe a Tick event of the System.Timer - that is called on its own thread - will be dangerous.

If you have an EcoSpace that only should be accessed from the mainthread - use a DispatcherTimer

Copied from msdn: If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

If you use the AsyncSupportService you can either do :

EcoServiceHelper.GetAsyncSupportService(EcoSpace).DispatchTaskToMainThread(()=>{TheThingYouNeedDoing()

;});
or
EcoServiceHelper.GetAsyncSupportService(EcoSpace).PerformTaskAsync(()=>{TheThingYouNeedDoing();});





lbernas
 Posted 2014-02-03 12:01:44
Supreme Being

Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)Supreme Being - (5 887 reputation)

Group: Awaiting Activation
Last Active: 2014-03-11 13:18:59
Posts: 21, Visits: 264
Thanks for the answer!

We don't use AsyncSupportService in the application now, but might do later on.

Regards,
Linus Bernas

Similar Topics

Expand / Collapse

Reading This Topic

Expand / Collapse

Back To Top