Note that there are some explanatory texts on larger screens.

plurals
  1. POWMI searches with c# threading how to work more CPU efficiënt
    primarykey
    data
    text
    <p>I have a program that creates a thread to search the WMI (Win32 classes) to check for all kinds of system information. Now I create a thread for each search, but obviously when I use my combobox and quickly scroll or get to trigger multiple threads the cpu will spike, and even after closing the form the "commands" sent still go to the wmi provider causing a cpu spike for quite a while...</p> <p><strong>Question:</strong></p> <p>What would be the best way to limit the cpu usage / max threads created to prevent cpu spikes without closing the form. (I can send a kill process for the WMI provider process if I close the form, so that would stop it).</p> <p><strong>Image:</strong></p> <p><img src="https://i.stack.imgur.com/9lIML.png" alt="enter image description here"></p> <p><strong>Code:</strong></p> <pre><code>namespace Admin_Helper { public partial class frmHardwareInformation : Form { public frmHardwareInformation() { InitializeComponent(); } string searchQuery; private void cmbItemList_SelectedIndexChanged(object sender, EventArgs e) { var dctPropertyList = new Dictionary&lt;string, string&gt;(); //Store property name/value searchQuery = cmbItemList.SelectedItem.ToString(); //Search term new Thread(() =&gt; FindWMI(searchQuery, dctPropertyList, lstHwSearchList)).Start(); //Start thread for each search } private void FindWMI(string s, Dictionary&lt;string, string&gt; dct, ListView listView) { try { ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("select * from " + s); Invoke(new MethodInvoker(() =&gt; { listView.Items.Clear(); //Clear items to prevent endless list })); foreach (ManagementObject moObject in moSearcher.Get()) { if (moObject != null) //Gives errors if I don't check for null's.. { foreach (PropertyData propData in moObject.Properties) { if (propData.Value != null &amp;&amp; propData.Value.ToString() != "" &amp;&amp; propData.Name != null &amp;&amp; propData.Name != "") //More prevention of errors.. dct[propData.Name] = propData.Value.ToString(); } } } foreach (KeyValuePair&lt;string, string&gt; listItem in dct) { Invoke(new MethodInvoker(() =&gt; { listView.Items.Add(listItem.Key).SubItems.Add(listItem.Value); Application.DoEvents(); })); } } catch (Exception) { } //Mostly catches invalid searches nothing too bad so far } } </code></pre> <p>}</p> <h2>EDIT: Included code changes</h2> <p>*Added killing processes on form closing, creating a list and invoking the whole update in 1x.</p> <pre><code>private void FindWMI(string s, Dictionary&lt;string, string&gt; dct, ListView listView) { try { List&lt;ListViewItem&gt; itemsList = new List&lt;ListViewItem&gt;(); ManagementObjectSearcher moSearcher = new ManagementObjectSearcher("select * from " + s); Invoke(new MethodInvoker(() =&gt; { listView.Items.Clear(); })); foreach (ManagementObject moObject in moSearcher.Get()) { if (moObject != null) { foreach (PropertyData propData in moObject.Properties) { if (propData.Value != null &amp;&amp; propData.Value.ToString() != "" &amp;&amp; propData.Name != null &amp;&amp; propData.Name != "") dct[propData.Name] = propData.Value.ToString(); } } } foreach (KeyValuePair&lt;string, string&gt; listItem in dct) { ListViewItem lstItem = new ListViewItem(listItem.Key); lstItem.SubItems.Add(listItem.Value); itemsList.Add(lstItem); } Invoke(new MethodInvoker(() =&gt; { listView.Items.AddRange(itemsList.ToArray()); })); } catch (Exception) { } } private void frmHardwareInformation_FormClosed(object sender, FormClosedEventArgs e) { foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses()) { if (myProc.ProcessName == "WmiPrvSE") { myProc.Kill(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload