0

I've got a C#/Winforms program that needs to list the computer's network adapters on the initial form, so this is a simplified version of my function to put together that list:

void LoadNicList() 
{
    ManagementObjectSearcher mos = new ManagementObjectSearcher(@"SELECT * 
                                         FROM   Win32_NetworkAdapter 
                                         WHERE  Manufacturer != 'Microsoft'
                                         AND NOT ProductName LIKE '%Wireless%'
                                         AND NOT ProductName LIKE '%Wifi%'
                                         AND NOT ProductName LIKE '%Wi-Fi%'
                                         AND NOT PNPDeviceID LIKE 'ROOT\\%'");
    foreach (ManagementObject mo in mos.Get())
    {
        if (mo["MACAddress"] != null)
        {
            comboBox1.Items.Add(mo["name"].ToString());
        }
    }
}

I haven't included the try/catch or any of the other stuff that goes on just to keep it simple, but this function should compile and run. This function is called by Form1_Load(). The problem is, this can cause quite a long delay loading the form, and the normal async/await functions can't be used.

I found this MSDN article on running ManagementObjectSearcher asynchronously: https://msdn.microsoft.com/en-us/library/cc143292.aspx I'd like to start LoadNicList() in the background while the form starts with a "Loading" message in the combobox, and then populate the list once it's ready, but I can't figure out how. Is that doable?

Corey O
  • 15
  • 4
  • Have you tried using TaskFactory? It is available in .net4 but what you could do is on launch, start a task to run your LoadNics() method and either wait for it to complete before continuing or let it complete on its own time. – nGX May 13 '15 at 20:26
  • KPS, no I'd never heard of it, I'll see if I can make it work tonight! Thanks! – Corey O May 13 '15 at 20:30
  • you are calling the wrong Get() see [here](https://msdn.microsoft.com/en-us/library/0w3ak0sb(v=vs.100).aspx) – RadioSpace May 13 '15 at 20:30
  • See my answer for example code. @RadioSpace is there a reason you say that? – nGX May 13 '15 at 20:35
  • @KPS Get has an overload which is linked in my comment. personally I think the task is better cause you get to manage it. – RadioSpace May 13 '15 at 20:38
  • RadioSpace, the MSDN article I linked to was on using the Get() overload, but being new to C#/Winforms/multithreaded applications I can't figure out how to poll the observer in the background while the rest of the UI remains active. – Corey O May 14 '15 at 14:57

1 Answers1

1

Try using this instead of just LoadNicList()

    //create cancellation token for future use
            CancellationToken cancellationToken = new CancellationToken();

//uischeduler is used to update the UI using the main thread
            TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            Task.Factory.StartNew(() =>
                            {
                                LoadNicList();
                            }, cancellationToken, TaskCreationOptions.None, uiScheduler);
nGX
  • 1,038
  • 1
  • 20
  • 40
  • I just dropped that in and it sort of works, the main form pops up now, but some of the buttons have a delay drawing, and changes to the textbox don't show up until after LoadNicList() has finished. Thanks for the suggestion, I think this will get me going in the right direction. – Corey O May 14 '15 at 14:52
  • You could do something like this: var task = Task.Factory.StartNew(() => { LoadNicList(); }, cancellationToken, TaskCreationOptions.None, uiScheduler); Then you will be able to do a task.Wait() and wait for the NIC list to load before going forward – nGX May 14 '15 at 19:30