Can I use an Interlocked.* synchronization method to update a DateTime variable?
I wish to maintain a last-touch time stamp in memory. Multiple http threads will update
My approach is not one of the best, but you can use a string var to store a formatted date and then parse it back into datetime:
class x
{
string _lastHit;
void Touch()
{
Interlocked.Exchange( ref _lastHit, DateTime.Now.ToString("format your date") );
}
}
When you need to use this value, just parse into DateTime:
DateTime.Parse(_lastHit)
The parsing is always working, because the string is formatted using the DateTime
class, but you can use TryParse
to handle possible parsing errors