RMSP
RMSP

Reputation: 41

C# Threading: How to open main form after thread is closed

Forgive me if this question is a duplicate of others, I probably do not know how to word out my problem correctly that's why I could not find the appropriate answer.

I have a winforms C# application with multiple forms. My main form is a login form, prior to it is a splash screen which loads first. My problem is that whenever I click logout, I want it to return to the login screen. However, the splash screen loads and does not open the login form anymore.

I suspect that this is because I started a new thread to be able to close the mainform and open the next form? I read it somewhere that I have to do that because an error occurs when merely using main.close() and form1.show(). Help?

SPLASH SCREEN CODE

public SplashScreen()
{
   InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
    progressBar1.Increment(1);
    if (progressBar1.Value == 100) timer1.Stop();
}

LOGIN START

public LOGIN()
{
   Thread t = new Thread(new ThreadStart(SplashScreen));
   t.Start();
   Thread.Sleep(5000);
   InitializeComponent();
   t.Abort();
}

public void SplashScreen()
{
   Application.Run(new SplashScreen());
}

LOGIN EXIT (Redirected to user's home page)

public static void OpenHomeAdmin() // new thread to open home ADMIN
{
    Application.Run(new Home_Admin());
}

if (usertype == "UT1") //admin rights
{
     //GET LOGGED USER
      Home_Admin homeAdmin = new Home_Admin();
      homeAdmin.SetUsername(username);

      this.Close();
      System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(OpenHomeAdmin));
      t.Start();
 }

Upvotes: 1

Views: 3016

Answers (2)

Hariharan Anbazhagan
Hariharan Anbazhagan

Reputation: 1329

I too had the same requirement and I wrote the following Code!

In Program.cs under Main()

Application.Run(new Splash()); //Splash Screen if u wanna run BEFORE Login Form

/*Showing Login Form Before Entering the Main Form [Authentication]*/

login f1 = new login();
DialogResult dr = f1.ShowDialog();
if (dr == DialogResult.OK)
{             
  Application.Run(new Splash()); //Splash Screen if u wanna run AFTER Login Form
  Application.Run(new Home_Admin());
}
else
{
  Application.Exit();
} 

in Login Form - After satisfying the authentication, write the following code

this.DialogResult = DialogResult.OK;
this.Close();

This will close your Login Form and get back to Program.cs, then will load your Home_Admin.

in Home_Admin make a Logout button and write the following code in click_event

this.Close();
login f1=new login();
f1.Show();

now u ll be logged out and u ll be back to login screen.

Note: Dont use this.Close(); in Main Form, this may exit your application. U can use this.Hide(); in such a chase.

Hope this ll help U. Try this and Get back with your Comments.

Upvotes: 1

Michael Hornfeck
Michael Hornfeck

Reputation: 1252

I don't believe you need to start a new Thread in order to open/close your forms. Try the following code instead:

if (usertype == "UT1") //admin rights
{
     //GET LOGGED USER
      Home_Admin homeAdmin = new Home_Admin();
      homeAdmin.SetUsername(username);

      this.Hide();
      homeAdmin.Show();
 }

Note that Form.Close() will dispose the Form object, meaning that all resources created within the object are closed as well. Read more here. Form.Hide() will alternatively hide the form from the user's view.

Upvotes: 0

Related Questions