0

I have a chat client (C# WPF Application) and a chat service (C# Web Service) which runs on a server. Since an application that gets killed by task manager or crashes cannot execute code I implemented my own way of letting the service know when a user goes offline, even if he crashes or kills the application:

The client sends every 4 seconds (configurable) a "keep-me-alive" request, which when received by the server turns that user's "KeepAlive" bool property to true. The service has a timer and when it elpases after 10 seconds (configurable) it calls a method called "AutoKick", which checks every user's KeepAlive.

If KeepAlive is false then it sets the user offline and kicks him out from all the chatrooms until he logs in again or sends a keep-me-alive request. If KeepAlive is true then nothing happens to the user but KeepAlive is set to false causing him to get kicked if no keep-alive requests are sent in the next 10 seconds.

When the user closes the program manually it calls a method to tell the service to set him offline immediately and kick him. If the client crashes/is killed/loses internet connection then the AutoKick notices after 10-20 seconds and sets the user offline. So far everything works as planned.

The only problem is that when the user turns on sleep-mode, or simply enters sleep mode after being away from the PC for too long, no keep-me-alive requests can be sent, because as far I know no code can be executed during sleep, this causes the user to get kicked and set offline.

I don't want the user to be set to offline because he's away from PC, instead I simply want the "Status" property (enum) to be changed from "Online" to "Away".

Is there any way to differentiate between sleep mode (not working as intended) and the rest of the causes for connection loss? I'm open for any ideas regarding changing the whole auto-kick thing, as I frankly have no idea how other chat applications handle this kind of thing.

Swapper
  • 107
  • 7
  • 1
    You can subscribe to the `PowerModeChanged` event, and then send a signal from the client to the service that the computer is going to sleep if the power mode is `PowerModes.Suspend`. Either [This answer](https://stackoverflow.com/questions/18206183/event-to-detect-system-wake-up-from-sleep-in-c-sharp/18206418#18206418) or [this answer](https://stackoverflow.com/questions/20119257/connected-standby-notification-for-a-w8-service/55676206#55676206) should help – Rufus L Dec 13 '19 at 16:40
  • Or [this answer](https://stackoverflow.com/questions/2208595/c-sharp-how-to-get-the-events-when-the-screen-display-goes-to-power-off-or-on/2208720#2208720) – Rufus L Dec 13 '19 at 16:47

0 Answers0