以下是您的選擇 - 在您的LoginForm
上定義LoginExpired
事件。在計時器滴答事件處理程序引發此事件:
public partial class LoginForm : Form
{
public event EventHandler LoginExpired;
public LoginForm()
{
InitializeComponent();
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
OnLoginExpired();
}
protected virtual void OnLoginExpired()
{
if (Visible)
return; // if this form visible, then user didn't authenticate yet
if (LoginExpired != null)
LoginExpired(this, EventArgs.Empty);
}
}
然後在此事件訂閱您Main
方法:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (LoginForm loginForm = new LoginForm())
{
if (loginForm.ShowDialog() != DialogResult.OK)
return;
loginForm.LoginExpired += new EventHandler(loginForm_LoginExpired);
Application.Run(new MainForm());
}
}
static void loginForm_LoginExpired(object sender, EventArgs e)
{
LoginForm loginForm = (LoginForm)sender;
if (loginForm.ShowDialog() != DialogResult.OK)
throw new NotAuthenticatedException();
}
從我的經驗'ShowDialog'不會讓你訪問其他形式...... – Shai 2012-07-12 10:21:30
能你表現出如何做到這一點?那麼你在'Application.Run'中做了什麼? – V4Vendetta 2012-07-12 10:26:49
你爲什麼要在窗體中調用'ShowDialog',你想要顯示? – 2012-07-12 10:34:35