static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); // Get initial settings var appSettings = ConfigurationManager.AppSettings; try { interval = Convert.ToInt32(appSettings["interval"]); FileLogger.Log("Using interval from app.config: " + interval, 1); } catch { interval = 60000; FileLogger.Log("Using default interval of 60000: " + interval, 1); } try { timeServerURL = appSettings["TimeServerURL"]; FileLogger.Log("Using " + timeServerURL + " from App.Config", 1); } catch { timeServerURL = "http://timeserver.tomeofjamin.net:20145"; FileLogger.Log("Issue in App.Config entry for <add key=\"TimeServerURL\" value=\"http://timeserverurl\"/>. Using " + timeServerURL, 1); } try { LogLevel = Convert.ToInt32(appSettings["LogLevel"]); FileLogger.Log("Using LogLevel from app.config: " + LogLevel, 1); } catch { LogLevel = 1; // Default log level FileLogger.Log("Using default log level: " + LogLevel, 1); } try { audioVolumeThreshold = Convert.ToInt32(appSettings["audioVolumeThreshold"]); FileLogger.Log("Using audioVolumeThreshold from app.config: " + audioVolumeThreshold, 1); } catch { audioVolumeThreshold = 0.005f; FileLogger.Log("Using default audioVolumeThreshold: " + audioVolumeThreshold, 1); } httpHandler.setTimeServerUrl(timeServerURL); // Set up a timer that triggers every minute. timer = new System.Timers.Timer(); timer.Interval = interval; timer.Elapsed += OnTimer; timer.Start(); // Register event for when user locks the system sseh = new SessionSwitchEventHandler(SysEventsCheck); SystemEvents.SessionSwitch += sseh; // Use application context so we can exist only in the system tray Application.Run(new MyCustomApplicationContext()); }
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { // Log the exception, display it, etc FileLogger.Log((e.ExceptionObject as Exception).Message, 1); }
// Every interval, if system is not idle, post interval time to the server. private static void OnTimer(object source, System.Timers.ElapsedEventArgs arg) { if (!sessionLocked) { long idleTime = 0; string userName = Environment.UserName; //string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; string hostName = Environment.MachineName; // Get duration where user has no input (idle time) try { idleTime = IdleTime.IdleTime.GetIdleTime(); } catch (Exception e) { FileLogger.Log(e.Message, 1); } // Check if any audio is playing, which is used to indicate user is consuming media // In that case, poll the server with idle value 0, because we assume the user is not idle. if (IsAudioPlaying(GetDefaultRenderDevice())) { FileLogger.Log("User is active with audio stream. Polling to server", 1); Activity thisActivity = new Activity(userName, hostName, DateTime.Now, Activity.activityType.idle, interval); try { sendActivity(thisActivity); MyCustomApplicationContext.noError(); } catch { MyCustomApplicationContext.setError(); } } // If the idle time is more than the interval, then we can assume that the user is not active else if (idleTime < interval) // Poll only if idle time is less than the defined poll interval { Activity thisActivity = new Activity(userName, hostName, DateTime.Now, Activity.activityType.idle, interval); try { sendActivity(thisActivity); MyCustomApplicationContext.noError(); } catch { MyCustomApplicationContext.setError(); } } else // No need to send a poll if we are idle > 2x polling interval { FileLogger.Log("Ignoring poll, idleTime: " + idleTime, 1); } } else { FileLogger.Log("Session is locked, skipped polling", 1); } }
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { // Log the exception, display it, etc FileLogger.Log(e.Exception.Message, 1); }