Exemplo n.º 1
0
 /// <summary>
 /// Wait for the completion event or until the timeout datetime has passed.
 /// </summary>
 /// <param name="timeout">The datetime at which a timeout exception should be raised.</param>
 public void WaitForCompletion(DateTime timeout)
 {
     // Wait until the operation has completed or we time out.
     while (!Complete && DateTime.Now < timeout)
     {
         Thread.Sleep(25);
     }
     // If the operation did not complete then we must have timed out.
     if (!Complete)
     {
         observer.Cancel();
         throw new Exception("The WMI operation did not complete within the time limit.");
     }
     // The operation completed, but we must check its status.
     if (Status != "NoError")
     {
         throw new Exception("The WMI operation encountered an error. " + Status.ToString());
     }
 }
Exemplo n.º 2
0
 private void cancelWorker_DoWork(object sender, DoWorkEventArgs e)
 {
     queryObserver.Cancel();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a list of Dictionaries mapping the property name to the object retrieved
        /// for that property.
        /// A list is returned because some WMI call return multiple things
        ///
        /// Was recently updated to handle the WMI calls in an asynchronous way, so the
        /// data collection can be stopped during a long-running WMI call.
        ///
        /// https://msdn.microsoft.com/en-us/library/cc143292.aspx
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        public RetrievalContext Retrieve(ILog log)
        {
            string query_str = string.Format("SELECT {0} FROM {1}", Context.Properties, Context.Class);

            if (string.IsNullOrEmpty(Context.Where) == false)
            {
                query_str += " WHERE " + Context.Where;
            }
            string path = string.Empty;

            try
            {
                SelectQuery     query = new SelectQuery(query_str);
                ManagementScope scope = Context.GetManagementScope();
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
                {
                    path = scope.Path.Path;
                    if (log != null)
                    {
                        log.Debug("Executing: " + path + " -- " + query_str);
                    }

                    ManagementOperationObserver results = new ManagementOperationObserver();
                    results.ObjectReady += new ObjectReadyEventHandler(this.OnNewObject);
                    results.Completed   += new CompletedEventHandler(this.OnComplete);

                    // Start a new Retrieving object so everything is up-to-date right now
                    m_retrieval_context = new RetrievalContext(Options.Timeout);

                    searcher.Get(results);

                    while (m_retrieval_context.IsCompleted == false)
                    {
                        if (m_retrieval_context.HasTimedOut)
                        {
                            results.Cancel();
                            throw new Exception($"Timeout executing '{query_str}', path is '{path}'");
                        }

                        if (GlobalIsRunning.IsRunning == false)
                        {
                            m_retrieval_context.IsCompleted = true;
                            results.Cancel();
                        }
                        else
                        {
                            Thread.Sleep(250);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    log.Error(e);
                    log.Error(query_str);
                    if (string.IsNullOrEmpty(path) == false)
                    {
                        log.Error(path);
                    }
                }
            }
            finally
            {
                m_retrieval_context.IsCompleted = true;
            }

            return(m_retrieval_context);
        }