This question follows on from the development of an issue originally posted here. The issue is with editing the properties of an object from multiple-threads.
The context of the application is as follows:
I have a System.Timers.Timer
object, which does the below numbered items on every tick. I need this to be done with a timer because I may want to be able to vary the tick interval from 1ms to 60s.
On every tick event:
- Run a background worker to read data from a file [order of ms] (or a URL [order of seconds])
- When background worker finishes reading data from file, raise an
Event (say
Ticker_Updated_Event
) - In the event handler of
Ticker_Updated_Event
, run object code which updates a certain property (saythis.MyProperty
). Additional code is executed in this event handler, including calls to update the GUI [which I believe I have made thread safe (at least i hope)].
Various suggestions have been made including using shared variables and interlocking, but I feel that the quickest(?) and potentially the most robust solution in terms of keeping the rest of my code functional, would be one where I simply force Timer
to wait for the code in its tick handler to finish before executing another tick. Can this be as simple as attaching a boolean expression to the start and end of the tick handler?
EDIT 1: I would like the timer to tick as soon as code has finished executing in its handler. I am interested in the performance of my application when running on 1ms tick intervals. I expect the code in the event handler to execute fairly quickly and do not see this being an issue when using it with intervals greater than say 200ms.
EDIT 2: I think I wasn't very clear in setting out the scope of my functionality. I essentially will have 2 states under which my program will run. At any run time, the program will either be:
- Using tick intervals >> time needed for execution of code in the handler, which will include doing http requests etc, so no issues here.
OR
- Using very short ticks ~1ms to read data from file and run the exact same code as in the first scope, where a loop would easily suffice but mean having to modify the code in the first scope item.