示例#1
0
        /// <summary>
        ///     Creates the Warden service which monitors processes on the computer.
        /// </summary>
        /// <param name="options"></param>
        public static void Initialize(WardenOptions options)
        {
            if (!Privileges.IsUserAnAdministrator())
            {
                throw new WardenManageException(Resources.Exception_No_Admin);
            }
            WardenImpersonator.Initialize();
            Stop();
            Options = options ?? throw new WardenManageException(Resources.Exception_No_Options);
            try
            {
                ShutdownUtils.RegisterEvents();


                _wmiOptions = new ConnectionOptions
                {
                    Authentication   = AuthenticationLevel.Default,
                    EnablePrivileges = true,
                    Impersonation    = ImpersonationLevel.Impersonate,
                    Timeout          = TimeSpan.MaxValue
                };

                _connectionScope = new ManagementScope($@"\\{Environment.MachineName}\root\cimv2", _wmiOptions);
                _connectionScope.Connect();

                var creationThreadStarted = new ManualResetEvent(false);
                CreationThread = new Thread(StartCreationListener)
                {
                    IsBackground = true
                };
                CreationThread.Start(creationThreadStarted);

                var destructionThreadStarted = new ManualResetEvent(false);
                DestructionThread = new Thread(StartDestructionListener)
                {
                    IsBackground = true
                };
                DestructionThread.Start(destructionThreadStarted);


                creationThreadStarted.WaitOne();
                destructionThreadStarted.WaitOne();
                Initialized = true;
                Logger?.Info("Initialized");
            }
            catch (Exception ex)
            {
                throw new WardenException(ex.Message, ex);
            }
        }