/// <summary> /// Prepares the Query UI and executes a WQL query via a new ManagementQueryBroker. /// </summary> /// <param name="query">The WQL query to execute.</param> private void ExecuteQuery(String query) { // Reset UI this.ResetQueryResults(); this.ToggleQueryUI(true); this.Log(LogLevel.Information, String.Format("Executing query: {0}", query)); try { // Prepare the query broker this.queryBroker = new ManagementQueryBroker(this.txtQuery.Text, this.CurrentNamespaceScope); this.queryBroker.ObjectReady += new BrokerObjectReadyEventHandler(this.OnQueryResultReady); this.queryBroker.Completed += new BrokerCompletedEventHandler(this.OnQueryCompleted); // Init the grid view this.InitQueryResultGrid(this.queryBroker.ResultClass, query); // Fetch results this.queryBroker.ExecuteAsync(); } catch (Exception e) { bool hasCode = false; UInt32 code = 0; if (e.GetType() == typeof(COMException)) { hasCode = true; code = (UInt32) ((COMException)e).ErrorCode; } else if(e.GetType() == typeof(ManagementException)) { hasCode = true; code = (UInt32) ((ManagementException)e).ErrorCode; } if (hasCode && Enum.IsDefined(typeof(ManagementError), code)) { var constant = ((ManagementError)code).ToString(); this.Log(LogLevel.Critical, String.Format("Query execution failed with {0:G} (0x{0:X}) {1}", code, constant)); var description = ErrorCodes.ResourceManager.GetString(constant); if (!String.IsNullOrEmpty(description)) this.Log(LogLevel.Information, description); } else { this.Log(LogLevel.Critical, String.Format("Query execution failed with: {0}", e.Message)); } // Generic exceptions may not trigger a 'Completed' event this.ToggleQueryUI(false); } }
/// <summary> /// Refreshes the query tab for the specified ManagementClass. /// </summary> /// <param name="c">The System.Management.ManagementClass to be displayed.</param> /// <remarks>Cancels any currently running queries.</remarks> private void RefreshQueryView(ManagementClass c) { // Stop an existing query if (this.queryBroker != null) { this.queryBroker.Cancel(); } if (c != null) { var query = c.GetDefaultQuery(); this.txtQuery.Text = query; this.queryBroker = new ManagementQueryBroker(query, this.CurrentNamespaceScope); InitQueryResultGrid(c, query); } }