Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

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.
Posted

Do you really understand why you want to use Invoke/BeginInvoke? These methods are implemented in System.Windows.Forms.Control (and hence in System.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 methods InvokeeginInvoke 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. With Invoke, a calling thread is put to wait state until the call is complete by the UI thread while BeginInvoke 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 need InvokeRequired 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 case System.Windows.Forms.Control.InvokeRequired will always return false, in the second case — always true. 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 class System.Windows.Threading.Dispatcher with the invocation methods of the same names. This class can be used for System.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
   
v2
Comments
Bert Mitton 3-Oct-11 14:00pm
   
Argh! You got your answer in first!

Still, it gets a 5. :)
   
[Irrelevant, removed -- SA]
Espen Harlinn 3-Oct-11 14:56pm
   
Excellent reply, Sergey! 5'ed!
   
Thank you, Espen.
--SA
Simon Bang Terkildsen 4-Oct-11 2:30am
   
Good thorough answer, my 5
You'll add this code into the Form1 class:

VB
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


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:

VB
ParentForm.finish("New Textbox1 Text")
   
Comments
   
Problem fixed, so I voted 5.
--SA
eljecto 5-Oct-11 7:51am
   
You say I need to pass form1 object. Where can I get form1 object.

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900