Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoke & Invoke.Required 1

Status
Not open for further replies.

V00D00

Technical User
Joined
Jul 11, 2005
Messages
78
Location
US
Can someone give me an example of invoke and invoke.required used to show and hide a form?

VD
 
InvokeRequired bascially tells you if the thread that is calling the method is the thread where the gui component exists.

So if you are calling a method on a GUI item, you check if InvokeRequired is true. If it is, you invoke the call on itself using a delgate.


public void DisplayMyForm()
{
if (this.InvokeRequired)
{
this.Invoke(new DisplayEventHandler(DisplayMyForm), new object[]);
}
else
{
this.show;
}
}

public delegate void DisplayEventHandler();
 
Please excuse my ignorance with this. Let me give you my example. My main form is checking for a connection state with a network service.

void AASRef_CallStatus(string Message)
{
switch (Message){
case "Attached":
loginpage.Show();
break;
case "Logged on":
loginpage.hide();
break;
}
}

I am trying to apply the example that you posted but am a little unsure about how exactly to do it. Using the code provided can you please demonstrate?
 
Add new methods to your LoginPage Form called DisplayForm() and HideForm().

Add the code I provided above in each of these methods. In the DisplayForm() use this.Show() In the HideForm() use this.Hide() or this.Close()
 
I should also add that this is a second form I am trying to invoke not the main form.
 
Doesn't make any difference.

The key thing to know here is those methods are used when the calling code is running on a different thread than the one that created the form.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top