-1

I'm writing a web-api and I plan on storing "sessions" of data in there. I want to call a method every hour to make sure that inactive sessions are ended.

The Database contains 3 timestamp fields. Session_Start, Session_End and Last_Activity. Everytime data in the session is updated, the Last_activity field updates.

I wrote a method to end inactive sessions, but how do I call that method every hour in the background, without interfering with the functionality of the webapi (how do I run the code in the background and still allow people to use the webapi)?

Jannik
  • 401
  • 1
  • 5
  • 17

2 Answers2

0

Sounds like a secondary thread with a sleep-wake life cycke of one hour should do the trick. Did you consider that? On a project I had not long ago I had the same debate. We eventually wrote a refresh method and called it every time a certain form was loaded, and executed the code upon checking the time . A bit sideways, but was acceptable on the project's scope.

yoad w
  • 315
  • 3
  • 10
0

I think you could just use a Timer.

aTimer = new System.Timers.Timer(new System.TimeSpan(1,0,0).Milliseconds);
aTimer.Elapsed += ClearSessions;
aTimer.AutoReset = true;
aTimer.Enabled = true;
michauzo
  • 356
  • 1
  • 9