帮助我使用ado.net中的datagridview.invoke方法 [英] help me out with datagridview.invoke method in ado.net

查看:136
本文介绍了帮助我使用ado.net中的datagridview.invoke方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要在sql server数据库中进行任何更改时得到通知,而windows窗体应用程序中datagridview中的数据需要显示表中存在的当前行,并通知我已经完成了更改我书中的database.example有代码来执行所需的任务但我感到困惑的地方是应用程序使用datagridview1.Invoke()方法。



示例: -

dataGridView1.Invoke((datagrid)委托(DataTable表){dataGridView1.DataSource = table;},dt);



datagrid是我之前已声明的委托的名称,dt对DataTable对象有参考。



有人可以详细解释我发生了什么datagridview1.Invoke()方法。

my application needs to be notified when any changes are made in the sql server database and the data in the datagridview in my windows form application needs to show the current rows present in the table also notifying me that a change has been done in the database.example in my book has the code to perform the desired task but where i am confused is where the application uses the datagridview1.Invoke() method.

EXAMPLE:-
dataGridView1.Invoke((datagrid)delegate(DataTable table) { dataGridView1.DataSource = table; }, dt);

datagrid is the name of my delegate that i have declared earlier and dt has the refrence to the DataTable object.

can someone explain me in details what''s happening in the datagridview1.Invoke() method.

推荐答案

方法 System.Windows.Forms.Control.Invoke System.Windows.Forms.Control.BeginInvoke 带del传递egate实例,以及传递给委托实例调用所需的参数数组,并将此数据排入UI线程读取的某个队列中。这样,调用就完成了一个UI线程。只有从UI线程之外的某个线程调用调用方法时才需要此机制。 (事实上​​,它也可以用作修改事件处理顺序的技巧,但应该小心使用。)



正如你所说的没有关于线程,这可能意味着你徒劳地使用调用。请检查一下。



如需更详细的解释,请查看我过去的答案:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

< a href =http://www.codeproject.com/Answers/159938/Problem-with-Treeview-Scanner-And-MD5#answer2> Treeview扫描仪和MD5的问题 [ ^ ]。



如果你阅读我的简短文章,我会介绍非UI(自定义,用户编写)线程的类似机制,你也可以很好地了解它是如何在内部实现的。请参阅:

线程的简单阻塞队列通信和线程间调用 [ ^ ]。



-SA
The methods System.Windows.Forms.Control.Invoke and System.Windows.Forms.Control.BeginInvoke take the delegate instance passed, as well as the array of parameters needed to pass to delegate instance call, and queue this data in some queue read by the UI thread. This way, the call is done be a UI thread. This mechanism is only needed when you call invocation methods from some thread other then the UI thread. (In fact, it can also be used as a trick to modify order of event handling, but should be used with great care.)

As you say no word about threads, it may mean that you are using invocation in vain. Please check it up.

For explanation in some more detail, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

You can also get a good idea of how it is internally implemented, if you read my short article where I introduce similar mechanism for non-UI (custom, user-written) thread. Please see:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


这篇关于帮助我使用ado.net中的datagridview.invoke方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆