2013-08-04 13 views
4

我有一個MDIPrent窗體,這是我的主要形式。現在我通過單擊LogOut MenuStrip註銷Main_Form。在我的代碼中,我阻止了重複的實例。但是我得到這個錯誤。我搜索了很多東西,嘗試了很多東西,但錯誤並沒有消失。 下面是Program.cs的文件代碼:在單個線程上啓動第二個消息循環不是有效的操作。使用Form.ShowDialog代替

using System.Diagnostics; 
static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     LoggedInUser = string.Empty; 
     loginSuccess = false; 
     String thisprocessname = Process.GetCurrentProcess().ProcessName; 
     if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1) 
      return; 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     MyApplicationContext context = new MyApplicationContext(); 
     Application.Run(context); 

    } 
    public class MyApplicationContext : ApplicationContext 
    { 
     private Login_Form lgFrm = new Login_Form(); 
     public MyApplicationContext() 
     { 
       try 
       { 
        lgFrm.ShowDialog(); 
        if (lgFrm.LogonSuccessful) 
        { 
         ////lgFrm.Close(); 
         lgFrm.Dispose(); 
         FormCollection frm = Application.OpenForms; 
         try 
         { 
          foreach (Form fc in frm) 
           fc.Close(); 
         } 
         catch (Exception ex){} 
         Application.Run(new Main_Form()); 
        } 
       } 
       catch (Exception ex){} 
      } 
     } 
} 

下面是Login_Form

public bool LogonSuccessful 
    { 
     get 
     { 
      return Program.loginSuccess; 
     } 

     set 
     { 
      Program.loginSuccess = value; 
     } 
    } 

    private void BtnEnter_Click(object sender, EventArgs e) 
    { 
     Login_Form lgn = new Login_Form(); 
     Program.loginSuccess = true; 
     this.Hide(); 
     Program.LoggedInUser = TxtBxUserName.Text; 
    } 

下面的代碼是Main_Form

private void LogOutMenuItem_Click(object sender, EventArgs e) 
    { 
     Login_Form lgFrm = new Login_Form(); 
     lgFrm.LogonSuccessful = false; 
     Program.loggedOut = true; 
     Program.LoggedInUser = string.Empty; 
     this.Close(); 
     ////FormCollection frm = Application.OpenForms; 

     ////foreach (Form fc in frm) 
     ////{ 
     //// MessageBox.Show(fc.ToString()); 
     ////} 

     Program.MyApplicationContext context = new Program.MyApplicationContext(); 
     Application.Run(context); 
    } 

我已經使用方面,因爲我想使Main_Form成爲應用程序的唯一OpenForm。某處我有了使用上下文的想法。

+0

創建新的進程,而不是Application.Run(背景); 這不是一個很好的解決方案,但我會寫一個很好的,如果你告訴我你爲什麼要做Application.Run()在第一個 –

+0

我應該創建進程logOut或而不是Application.Run()?我通常做Application.Run()來啓動應用程序。 – DhavalR

+0

你想要達到 –

回答

0

試試這個:不是發射一個新的應用程序註銷時,只處理你的窗口,並替換此MyApplicationContext()

public MyApplicationContext() 
    { 
     bool isSuccessful = false; 
     do 
     { 
      try 
      { 
       lgFrm = new Login_Form(); 
       lgFrm.ShowDialog(); 
       if (lgFrm.LogonSuccessful) 
       { 
        isSuccessful = lgFrm.LogonSuccessful; 
        ////lgFrm.Close(); 
        lgFrm.Dispose(); 
        FormCollection frm = Application.OpenForms; 
        try 
        { 
         foreach (Form fc in frm) 
          fc.Close(); 
        } 
        catch (Exception ex){} 
        Application.Run(new Main_Form()); 
       } 
      } 
      catch (Exception ex){} 
     }while(isSuccessful); 
    } 
+0

同一個地方出現同樣的錯誤。 Login_Form剛剛閃過和錯誤。 – DhavalR

+0

你究竟在哪裏得到錯誤cus'只是運行一個程序使用這段代碼,並沒有得到錯誤。你是否刪除了Application.Run(context); ? –

+0

我在Application.Run(new Main_Form())獲取它;在MyApplicationContext中。 – DhavalR

3

你的例外是因爲你叫Application.Run(...)內的另一個Application.Run(...),修改如下:

//MyApplicationContext constructor 
public MyApplicationContext() 
    { 
      try 
      { 
       lgFrm.ShowDialog(); 
       if (lgFrm.LogonSuccessful) 
       { 
        ////lgFrm.Close(); 
        lgFrm.Dispose(); 
        FormCollection frm = Application.OpenForms; 
        try 
        { 
         foreach (Form fc in frm) 
          fc.Close(); 
        } 
        catch (Exception ex){} 
        //Application.Run(new Main_Form()); <<<---- Remove this 
        MainForm = new Main_Form(); 
       } 
      } 
      catch (Exception ex){} 
      //Add the ThreadExit event handler here 
      ThreadExit += (s,e) => { 
       if(Program.loggedOut) { 
       Program.MyApplicationContext ctxt = new Program.MyApplicationContext(); 
       Application.Run(ctxt); 
       } 
      }; 
     } 
    } 
// 
private void LogOutMenuItem_Click(object sender, EventArgs e) 
{ 
    Login_Form lgFrm = new Login_Form(); 
    lgFrm.LogonSuccessful = false; 
    Program.loggedOut = true; 
    Program.LoggedInUser = string.Empty; 
    this.Close(); //I think you want to call Application.Restart() here? 
        //if so, you don't need the ThreadExit event handler added in the MyApplicationContext() constructor. 
} 
+0

我調用Application.Run(context);運行良好。但是當我嘗試註銷時,出現錯誤。 – DhavalR

+0

@DhavalR只是因爲你在'LogOutMenuItem_Click'中有'Application.Run(context)'這一行,你爲什麼需要它?只要刪除它,如果你想顯示某種形式,直接顯示它。 –

+0

Application.Run()也在Main()中。因爲我不想Login_Form在LoginSuccess之後保持打開狀態。 LoginForm.ShowDialog()在上下文中(即在Program.cs中,參見問題中的代碼)。 – DhavalR

0

我有同樣的問題

解決這個最好的辦法是使用

Application.Restart(); 

你應該使用它當過用戶註銷,那麼近

主界面和登錄對話框出現!

但與登錄表格ü得使用這種

ControlBox = False; 

所以用戶不能通過關閉它繞過它!! 簡單地U可以用代碼

Application.Exit(); 

添加退出按鈕,這是我如何與任何額外Usings或錯誤解決了這個問題...

相關問題