-1

如何分离线程?

我首先使用实例运行LoginForm.cs,所以,我在应用程序中的第一个线程是LoginForm.cs文件,但我不想再运行LoginForm.cs的线程,登录成功后,我希望我的应用程序运行主线程MainInterface.cs,意思是,首先Thread运行LoginForm.cs,然后在LoginForm.cs上停止Thread,登录更正后,线程运行在MainInterface.cs中。

我跟着下面的代码,但 LoginForm.cs 仍然是一个主线程:

主界面.cs

using System;

public class MainInterface : Form
{
   private static MainInterface Current;

   private MainInterface ()
   {
      if ( LoginForm.Instance != null )
         LoginForm.Instance.Close ();
   }

   public static MainInterface Instance
   {
      get 
      {
         if (Current == null)
         {
            Current = new MainInterface ();
         }
         return Current;
      }
   }
 }

登录表单.cs

using System;

public class LoginForm: Form
{
   private static LoginForm Current;

   private LoginForm ()
   {

      if ( MainInterface.Instance != null )
         MainInterface.Instance.Close ();
   }

   public static LoginForm Instance
   {
      get 
      {
         if (Current == null)
         {
            Current = new LoginForm ();
         }
         return Current;
      }
   }
}

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Myapp
{
   static class Program
   {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            LoginForm.Instance.ShowDialog ();

        }
    }
}
4

1 回答 1

0

程序.cs

using (Login login = new Login())
        { 
            login.ShowDialog(); //Close this form after login is correct in login form button_click
        }
        if (isValiduser == true) //Static variable created in application to check user
        {           
             Application.Run(new MainInterface());

        }

登录表单点击事件

    private void btnLogin_Click(object sender, EventArgs e)
            {
              if(isValiduser == true)
               {
                  this.Close();
               }
             else
               {
                  //else part code
               }
            }
于 2012-06-13T09:40:49.533 回答