Advertisement
Guest User

system service

a guest
Mar 24th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. /*
  2.  * Сделано в SharpDevelop.
  3.  * Пользователь: Андрей Михайлович
  4.  * Дата: 18.03.2015
  5.  * Время: 10:11
  6.  *
  7.  * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Diagnostics;
  14. using System.ServiceProcess;
  15. using System.Text;
  16. using Microsoft.Win32;
  17.  
  18. namespace scrblckService
  19. {
  20.     public class scrblckService : ServiceBase
  21.     {
  22.         public const string MyServiceName = "scrblckService";
  23.        
  24.         public scrblckService()
  25.         {
  26.             InitializeComponent();     
  27.         }
  28.  
  29.         void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
  30.         {
  31.             Microsoft.Win32.SystemEvents.SessionSwitch += new Microsoft.Win32.SessionSwitchEventHandler(SystemEvents_SessionSwitch);
  32.             if ((e.Reason == SessionSwitchReason.SessionLock) || (e.Reason == SessionSwitchReason.SessionLogoff))
  33.             {
  34.                 //I left my desk
  35.                 Process[] proc = Process.GetProcessesByName("ScreenBlocker");
  36.                 foreach (Process prs in proc)
  37.                 {
  38.                     prs.Kill();
  39.                 }
  40.                 Process[] proc2 = Process.GetProcessesByName("ScreenStream");
  41.                 foreach (Process prs in proc2)
  42.                 {
  43.                     prs.Kill();
  44.                 }
  45.             }
  46.             else if ((e.Reason == SessionSwitchReason.SessionUnlock) || (e.Reason == SessionSwitchReason.SessionLogon))
  47.             {
  48.                 //I returned to my desk
  49.                 Process[] proc = Process.GetProcessesByName("ScreenBlocker");
  50.                 int count = 0;
  51.                 foreach (Process prs in proc)
  52.                 {
  53.                     count++;
  54.                 }
  55.                 if (count > 0)
  56.                 {
  57.                     try
  58.                     {
  59.                         Process.Start(@"C:\Program Files\ScreenBlocker\ScreenBlocker.exe");
  60.                     }
  61.                     catch
  62.                     {
  63.                         Process.Start(@"C:\Program Files (x86)\ScreenBlocker\ScreenBlocker.exe");
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.        
  69.         private void InitializeComponent()
  70.         {
  71.             this.ServiceName = MyServiceName;
  72.         }
  73.        
  74.         /// <summary>
  75.         /// Clean up any resources being used.
  76.         /// </summary>
  77.         protected override void Dispose(bool disposing)
  78.         {
  79.             // TODO: Add cleanup code here (if required)
  80.             base.Dispose(disposing);
  81.         }
  82.        
  83.         /// <summary>
  84.         /// Start this service.
  85.         /// </summary>
  86.         protected override void OnStart(string[] args)
  87.         {
  88.             // TODO: Add start code here (if required) to start your service.
  89.         }
  90.        
  91.         /// <summary>
  92.         /// Stop this service.
  93.         /// </summary>
  94.         protected override void OnStop()
  95.         {
  96.             // TODO: Add tear-down code here (if required) to stop your service.
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement