Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this below code

C#
frmLogin objfrmLogin = new frmLogin();
objfrmLogin.ShowDialog();
//Some more lines of code to check session
MessageBox.Show("Some message","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);


The weird problem I am facing is that instead of first showing the frmLogin form the code below it is being executed which checks if the session exists.

So the app always starts and exits without showing the login form.

What I have tried:

I commented the session check code below the form show code. It just shows the Main form with all controls disabled as it should as the session does not exist and after 15 secs the app exits.
Posted
Updated 20-Nov-22 5:55am
Comments
Richard MacCutchan 20-Nov-22 9:10am    
There is not enough context in your question to offer any guesses. But a few minutes with the debugger should show you the exact sequence of what is happening.

C#
frmLogin objfrmLogin = new frmLogin();
objfrmLogin.ShowDialog();
//Some more lines of code to check session
MessageBox.Show("Some message","Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

You have a problem in your fromLogin code somewhere or you're doing something with Tasks or threading incorrectly.

ShowDialog is a blocking call, so no code after it will execute until the dialog is dismissed. UNLESS, you're doing something very wrong in the fromLogin code or with tasks or threading.
 
Share this answer
 
There must be something in your frmLogin what causes this, maybe a Timer?
Without showing your code we can only guess.
You could try with a standard Windows form instead and see what happens.
Or put a breakpoint in your frmLogin and step through until you see an error that causes the form to close.
It can also help to comment out try .. catch constructions that prevent you seeing any errors.
 
Share this answer
 
v2
Comments
Christopher Fernandes 20-Nov-22 9:22am    
the frmLogin login first authenticates the credentials entered. Then if all is well it will create a session on the app server. After that the API code checks if the authentication session token exists for the current user.

There is no timer on the frmLogin form.
Graeme_Grant 20-Nov-22 9:28am    
see my answer above ... would wrap the check as an Async method so as not to block the UI thread...
Where is the logic to check if the user has signed in? Here is a tutorial video for you: How to Create Login Form in C# 2020 - YouTube[^]

UPDATED

Impossible to tell when as we do not know when you are executing the code. I would lock the form as shown below:
C#
private void Form1_Shown(object sender, EventArgs e)
{
    // do login here, after the form is loaded and shown.
}


Example:

1. Main form:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        frmLogin objfrmLogin = new frmLogin();
        objfrmLogin.ShowDialog();
    }

2. login form:
C#
public partial class frmLogin : Form
{
    public frmLogin()
    {
        InitializeComponent();
    }

    private void frmLogin_Shown(object sender, EventArgs e)
    {
        _ = DummyCheckinLogic();
    }

    private async Task DummyCheckinLogic()
    {
        await Task.Delay(1000).ConfigureAwait(false);
        // unload the login windows (self)
    }
}
 
Share this answer
 
v4
Comments
Christopher Fernandes 20-Nov-22 9:14am    
I want to know why the code below the objfrmLogin.ShowDialog(); executes before showing the form.
Christopher Fernandes 20-Nov-22 9:16am    
I cannot show you the code to check if the user has signed as it is a custom method used in multiple apps.
Graeme_Grant 20-Nov-22 9:21am    
see my update
Christopher Fernandes 20-Nov-22 9:46am    
There was an error on the login form which did not allow the form to show and just quit.
Graeme_Grant 20-Nov-22 9:52am    
I ran the code and it worked fine. What error are you seeing? Here is a downloadable version of the code above: WinformLogin.zip - Google Drive[^]

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