.net - Application Idle - Is this thread-safe? -
this should thread-safe, right? there maybe better way?
imports system.threading public class applicationidlehelper private shared lastinputticks long public shared sub setlastinputtime(dt datetime) interlocked.exchange(lastinputticks, dt.ticks) end sub public shared function getidletime() integer return (now - new datetime(interlocked.read(lastinputticks))).totalseconds end function end class in application i'm setting lastinputticks value method setlastinputtime in prefiltermessage event. in other threads have check variable. calling getidletime, i'm not sure, if thread-safe or can in trouble.
thank in advance!
your solution looks okay. access variable in question covered interlocked access should fine , don't receive corrupted values in getidletime function.
the thing should aware of may receive old value getidletime function because exchange new value not done. if setlastinputtime , getidletime called more or less @ same time, getidletime function may or may not show updated time.
in case not think there way done better this. overlapping call issue never rid of. nature of multi-threading.
Comments
Post a Comment