在其他类中调用 [英] invoke in other class
问题描述
我在调用时遇到问题,我有一个像这样的类,而在另一个里面
Hi, i have problem with invoke i have one class something like and inside another one
Public Class Form1
...........
...........
...........
public class client
...........
Private Sub finish(ByVal message As String)
If Me.InvokeRequired Then
Me.Invoke(New send(AddressOf finish), message)
Else
Form1.textbox1.text = message
End If
End Sub
End Class
end class
.....
end class
这给了我错误;调用;不是....
的成员
我不明白我在另一个类中如何使用invoke怎么了?
我得到了答案
那仅仅是因为您尚未实现一种称为Invoke的方法.我想这是故意的,因为您似乎认为类客户端继承了Control,该控件具有名为Invoke的方法.
因此,您将必须实现自己调用或从Control [^]继承.
同样,Form1.client.InvokeRequired
但是,当我在客户端类中放入继承控件"时,错误消失了,但是我似乎没有调用它.我想为文本框分配值,但是一旦添加继承控件",它就不会出现
答案
and it gives me error ;Invoke; is not a member of ....
I don''t understand what''s wrong how I can use invoke in another class ??
I got answer
That''s simply because you have not implemented a method called Invoke. I guess that is intentional as you seem to think your class client inherits Control which has a method called Invoke.
So you''ll have to either implement Invoke yourself or inherit from Control[^].
The same goes for Form1.client.InvokeRequired
However when I put in client class "inherits control" error goes away but I doesn''t seem for me to invoke. I want to assign value to textbox but it doesnt come up once i add "inherits control"
ANSWER
You have the finish method in class client? Move the code to Form1 because that is the Control that can determine InvokeRequired. It also seems that your class would not need to inherit from Control so it shouldn't.
You need to invoke when a thread wants to change the UI. The UI control function you call from the thread must be thread safe and would need to check if InvokeRequired and Invoke itself if needed.
Have a look here for more info:
Updating the UI from a thread - The simplest way[^]
抱歉,我一直在获取答案,但我只是不了解线程方面的新知识,可能有些人可能会举个例子给我.
Sorry i keep getting answers but i just dont understand am new on threading and invoke maybe some of you could jus give me example please.
推荐答案
您真的了解为什么吗要使用Invoke/BeginInvoke
吗?这些方法是在System.Windows.Forms.Control
中实现的(因此特别是在System.Windows.Forms.Form
中),当您从UI线程以外的线程为Control类调用它们时,仅有用 >.您不必实现那些方法,并且您实际上无法做到这一点.
为什么需要这些方法?除调用方法外,您永远不能从另一个线程调用任何UI方法或属性.方法Invoke
eginInvoke允许您将对其他一些与UI相关的方法的调用委派给UI类,而不是真正在非UI线程中调用它们.而是将委托实例和调用它们所需的数据(例如参数值)放在UI线程支持的某个队列中. UI线程拾取队列中的元素,并与其他UI调用同步执行实际的调用.使用Invoke
时,调用线程进入等待状态,直到UI线程完成该调用为止,而BeginInvoke
则在实际上未执行调用时立即返回.
使用哪个UI控件来调用调用方法并不重要.仅此控件是当前正在运行的UI的一部分是唯一重要的.现在,当您总是从非UI线程中调用某些方法时,实际上并不需要System.Windows.Forms.Control.InvokeRequired
-这是经常发生的情况–您专门从非UI线程中调用来开发某些方法. 在这种情况下,始终需要调用.如果您的调用方法本身是从其他线程调用的,则只需要在调用某些方法之前调用InvokeRequired
即可:有时是从UI线程,有时是从其他线程.在第一种情况下,System.Windows.Forms.Control.InvokeRequired
将始终返回false
,在第二种情况下-始终为true
.如果不需要调用,您仍然可以使用调用方法,但是直接调用将具有更好的性能.
侧面说明:对于WPF UI,可以通过具有相同名称的调用方法通过类System.Windows.Threading.Dispatcher
使用相同的机制.此类可以很好地用于System.Windows.Forms
.
参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.threading.dispatcher.aspx [ ^ ].
剩下的问题是:是否可以使用此机制或类似机制在任意线程上调用某些方法.答案是:否,除非您创建了这样的机制.这是我的提示/技巧文章,显示了如何实现此目的: Simple Blocking Queue用于线程通信和线程间调用 [ ^ ].
—SA
Do you really understand why you want to useInvoke/BeginInvoke
? These methods are implemented inSystem.Windows.Forms.Control
(and hence inSystem.Windows.Forms.Form
in particular) and are only useful when you call them for Control classes from a thread other than your UI thread. You don''t have to implement those methods and you really cannot do that.
Why do you need those methods? You can never call any UI methods or properties form another thread except invocation methods. The methodsInvoke
eginInvoke allow you to delegate calling of some other UI-related methods to UI class, not really calling them in your non-UI thread. Instead, the delegate instances and data needed to invoke them (such as values of parameters) are put in some queue supported by the UI thread. The UI thread picks up the elements of the queue and performs the actual call in sync with other UI calls. WithInvoke
, a calling thread is put to wait state until the call is complete by the UI thread whileBeginInvoke
returns immediately, when a call is not actually performed.
It is not really important on which UI control is used to call an invocation method; it''s only important that this control is a part of currently running UI. Now,System.Windows.Forms.Control.InvokeRequired
is not really needed when you always call some your method from non-UI thread, which is very often the case — you develop some method specially from calling from some non-UI thread. In this case invoke is always required. You only needInvokeRequired
to call before calling some method if your calling method is itself called from different threads: sometimes from your UI thread, sometimes from some other thread. In first caseSystem.Windows.Forms.Control.InvokeRequired
will always returnfalse
, in the second case — alwaystrue
. If invoke is not required you still can use invocation methods, but direct call will have better performance.
One side note: for WPF UI the same mechanism can be used via the classSystem.Windows.Threading.Dispatcher
with the invocation methods of the same names. This class can be used forSystem.Windows.Forms
we well.
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^].
The remaining question is: can this or similar mechanism be used to invoke some method on an arbitrary thread. The answer is: no, unless you create such mechanism. Here is my Tips/Trick article showing how to achieve that: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].
—SA
您将将此代码添加到Form1类中:
You''ll add this code into the Form1 class:
Private Delegate Sub FinishDelegate(ByVal Message As String)
Private Sub finish(ByVal message As String)
If Me.InvokeRequired Then
Dim a(0) As Object
a(0) = message
Dim del As New FinishDelegate(AddressOf finish)
me.Invoke(del, a)
Else
me.textbox1.text = message
End If
End Sub
您需要确保将form1对象传递给客户端(它需要知道将调用finish子例程的form1对象).假设您已经将form1对象传递给了客户端对象(假设它存储在名为ParentForm的变量中),那么当您要调用finish例程时,您只需要以下代码:
You''ll need to make sure the form1 object is passed to the client (it needs to know the form1 object which it will call the finish subroutine). Assuming you''ve passed the form1 object to the client object (pretend it''s stored in a variable called ParentForm), you''ll just need the following code when you want to call the finish routine:
ParentForm.finish("New Textbox1 Text")
这篇关于在其他类中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!