Reputation: 8350
I am trying to implement single instance of the form. Because my application is all about hiding and showing different form dynamically according to some runtime values.
when i want to show the Trainee_Login form from a class file, i do this...
Trainee_login ShowTraineelogin = Trainee.login.TraineeloginRef; ShowTraineelogin.ShowScreen();
when i want to hide the Trainee_Login form from a class file, i do this...
Trainee_login HideTraineelogin = Trainee.login.TraineeloginRef; HideTraineelogin.HideScreen();
Problem is the InvokeRequired is always false and the else condition gets executed. I am using the same pattern for other forms too, where the Invokerequired is true and if condition of showscreen() is executed. Same problem for Hidescreen() as well.
Am i missing something ??
Code in my Trainee_login form :
private static Trainee_Login Trainee_LoginInstance = null;
public static Trainee_Login Trainee_LoginRef
{
get
{
if (Trainee_LoginInstance == null)
Trainee_LoginInstance = new Trainee_Login();
return Trainee_LoginInstance;
}
}
public void showScreen()
{
if (this.InvokeRequired == true)
{
this.Invoke(new MethodInvoker(this.showScreen));
}
else
{
this.Show();
}
}
public void hideScreen()
{
if (this.InvokeRequired == true)
{
this.Invoke(new MethodInvoker(this.hideScreen));
}
else
{
this.Hide();
}
}
Upvotes: 1
Views: 3589
Reputation: 64218
Possible the Handle is not created, you can test this with IsHandleCreated. See this question for numerous issues on IvokeReqired usage. You can see the my own answer was far from simple to acually make it work reliably.
I would recommend first not threading the UI. If you must use the .Net reflector to see what those API calls are really doing.
Update:
@karthik, your sample isn't complete enough for me to be able to fix it. I can't tell when or on what thread the form might have been created. I can tell you that if you just call this from any-old-thead it won't work, it needs a message pump. There are thee ways to get a message pump on a thread:
Upvotes: 4
Reputation: 73564
InvokeRequired is only necessary when you're dealing with multiple threads. I don't see in your code where you'd be using more than one thread. Opening and closing forms all happens on the main UI thread, so it's just not necessary in your scenario.
Added
Just in case you need it, an introduction to threading can be found here:
http://www.yoda.arachsys.com/csharp/threads/
Upvotes: 2