Click here to Skip to main content
13,832,097 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a simple form with two buttons (Start and Stop) and a DataGridView (named Grid). I am trying to populate the DataGridView with the results of a WMI query that enumerates Win32_Process instances and puts all process names in the only column of the DataGridView. The code looks like this:

using System;
using System.Management;
using System.Windows.Forms;

namespace WindowsFormsApplication10
    public partial class Form1 : Form
        ManagementObjectSearcher Searcher =
            new ManagementObjectSearcher();

        SelectQuery Query = new SelectQuery();
        ManagementOperationObserver Observer =
            new ManagementOperationObserver();
        public Form1()
            Observer.Completed +=
                new CompletedEventHandler(Observer_Completed);
            Observer.ObjectReady +=
                new ObjectReadyEventHandler(Observer_ObjectReady);
            Grid.ColumnCount = 1;
            Grid.Columns[0].Name = "Name";

        private void Start_Click(object sender, EventArgs e)
            Query.QueryString = "Select * From Win32_Process";
            Searcher.Query = Query;


        private void Observer_Completed
            (object sender, CompletedEventArgs e)

        private void Observer_ObjectReady
            (object sender, ObjectReadyEventArgs e)
            string [] row = new string [] 


        private void stop_Click(object sender, EventArgs e)

When I run the code using the 'Start Debugging' option it runs fine and populates the DataGridView. One strange thing (at least to me) I noticed is that in the Observer_ObjectReady the Grid.Refresh() line is never reached. When I run the code with 'Start Without Debugging' the DataGridView is populated but the form freezes immediately after that. How can I deal with this? (Sorry if this is not enough information - I am willing to provide more if necessary, but, as you might have noticed, I don't have much experience with C# or Visual Studio).

I have been able to use the same asynchronous code and send the data to a RichTextBox, but I would also like to be able to populate a DataGridView. I suppose this has to do with a DataGridView, but I am puzzled because I don't get any exceptions, the application just freezes, while the same application works fine when I replace the DataGridView with a RichTextBox and add lines of text from Observer_ObjectReady.

(I have posted the same question on StackOverflow, but without a resolution)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


without having tried it I'm relatively sure that its a threading issue. GUI elements are not allowed to be changed from any other than the GUI thread (the thread which created the corresponding window handle).
Have a look at Control.Invoke[^].

So why does it work with the RichtTextBox? I don't know. The behaviour is nearly random when doing inter-thread-calls with GUI elements.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You are right, and I have been able to make it work using Control.BeginInvoke. Thank you for the answer.
er_deepak 31-Jan-13 3:23am
please show that code how u do that ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190114.1 | Last Updated 14 Dec 2009
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100