2012-06-14 4 views
-1

Program.csC# 스레딩 과정

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 
    } 
} 

MainInterface 양식. 실제로이 코드는 실행되지 않습니다 Application.Run(new MainInterface()); 그럼이 코드가 잘못된 점은 누구에게 말해 줄 수 있습니까?

+0

경우처럼한다 btnLogin_Click을 다음과 같이 할당하고 있습니까? 귀하의 로그인 대화 상자에있는 단추 중 하나에 대한 이벤트 처리기? –

+0

또한 스레딩을 다루지 않는 것으로 보이기 때문에 제목이 오도 된 것입니다. –

+0

답변 해 주셔서 감사합니다.하지만 저는 여전히 이해할 수 없습니다. –

답변

-1

제가 생각할 때 this.Close()에 도달하면 컨트롤이 주 스레드로 돌아가고 응용 프로그램이 끝납니다 (이후에 코드가없는 경우). 즉 프로그램은 주 행의 첫 번째 행에서 시작하여 마지막 행까지 끝납니다. 따라서 로그인 폼을 먼저 열면 로그인 폼을 닫기 전에 MainInterface 폼을 열어야합니다.

+0

프로그램이 메인 스레드로 돌아 가기 때문에 거기에서 다른 폼을 열 수 있습니다. – Damith

0

문제는 코드에 isValidusertrue으로 설정하지 않았습니까? 그래서 그것은 최신 실행 MainInterface 양식입니다.

static class Program 
{ 
    public static bool isValiduser = false; 

    [STAThread] 
    static void Main() 
    { 
     // rest of your code 

로 Program.cs 파일에 isValiduser라는 정적 변수를 정의 가정 그리고 당신은 로그인 버튼을 클릭 할 때 당신은 로그인 상태에 따라이 변수를 설정해야합니다. 이렇게하려면 별도의 방법이 필요할 수 있습니다.

private void btnLogin_Click(object sender, EventArgs e) 
{ 
    // call validate user method and set value to `isValiduser` 
    Program.isValiduser= IsValidUser("username", "password"); // here for testing i'm set it as true 
    if(Program.isValiduser== true) 
    { 
     this.Close(); 
    } 
    else 
    { 
     //else part code 
    } 
} 

당신이 사용자에게 확인하는 방법이있을 수

private bool IsValidUser(string name, string pw) 
{ 
    return true; // impliment this method 
} 
1

Program.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(); 
       this.DialogResult = DialogResult.OK; 
      } 
     else 
      { 
       //else part code 
      } 
     } 
+0

'this.Close()'와'this.DialogResult = DialogResult.OK' 프로그램이 메인 스레드로 되돌아 갈 때 아무 변화가 없습니다. – Damith