views:

239

answers:

2

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();
        }
    }
+2  A: 

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/

David Stratton
@David Stratton, this is a multi threaded application, There are no exceptions. Since the Invokerequired is False, my form is not showing up. I have not shown the whole code here, I am using more than 5 threads in my app.
Anuya
OK. That makes a difference.
David Stratton
@David Stratton, If the InvokeRequired is True, it is showing and hiding the form perfectly, which happens for my other forms. Only for this form, i am getting InvoleRequired is False. That is the issue..
Anuya
@David Stratton, Any changes in code will make it work ??
Anuya
Sorry, no... Not based on the code I'm seeing.
David Stratton
@David Stratton, Any Ideas ?
Anuya
+1  A: 

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:

  1. Call Application.Run
  2. Call Form.ShowModalDialog
  3. Call Application.DoEvents in a loop
csharptest.net
@csharptest.net, Can u make straight forward for my case ? i am new to multi threading and Invoke mothods.
Anuya
See updated response.
csharptest.net
PS: Quit using threads while your ahead, they are not worth it in WinForms, trust me I know :) if you need a background task use BackgroundWorker only.
csharptest.net