填充DataGridView之后,应用程序挂起 [英] Application Hangs after a DataGridView is Populated
问题描述
我有一个带有两个按钮(开始"和停止")和一个DataGridView(名为Grid)的简单表单.我正在尝试使用WMI查询的结果填充DataGridView,该WMI查询枚举Win32_Process实例,并将所有进程名称放在DataGridView的唯一列中.代码如下所示:
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()
{
InitializeComponent();
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;
Searcher.Get(Observer);
}
private void Observer_Completed
(object sender, CompletedEventArgs e)
{
Grid.Refresh();
}
private void Observer_ObjectReady
(object sender, ObjectReadyEventArgs e)
{
string [] row = new string []
{e.NewObject["Name"].ToString()};
Grid.Rows.Add(row);
Grid.Refresh();
}
private void stop_Click(object sender, EventArgs e)
{
Observer.Cancel();
}
}
}
当我使用开始调试"选项运行代码时,它运行良好并填充了DataGridView.我注意到的一件奇怪的事情(至少对我而言)是,在Observer_ObjectReady中,从未达到Grid.Refresh()行.当我使用不调试开始"运行代码时,将填充DataGridView,但此后窗体立即冻结.我该如何处理? (很抱歉,如果这还不够,我愿意在必要时提供更多信息,但是,正如您可能已经注意到的那样,我对C#或Visual Studio没有太多的经验.)
我已经能够使用相同的异步代码并将数据发送到RichTextBox,但我也希望能够填充DataGridView.我想这与DataGridView有关,但是我感到困惑,因为我没有得到任何异常,应用程序冻结了,而当我用RichTextBox替换DataGridView并从Observer_ObjectReady添加文本行时,同一个应用程序运行良好.
(我在StackOverflow上发布了相同的问题,但没有解决方法)
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)
推荐答案
如果没有尝试过,我就可以肯定它是线程问题.除了GUI线程(创建相应窗口句柄的线程)外,不允许将GUI元素更改为其他任何元素.
看看 Control.Invoke [
Hi,
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.
Robert
您是对的,我已经能够使用Control.BeginInvoke使它正常工作.谢谢您的回答.
You are right, and I have been able to make it work using Control.BeginInvoke. Thank you for the answer.
这篇关于填充DataGridView之后,应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!