Winforms UI冻结时加载新的from



我正在尝试制作一个异步计时器,将关闭所有打开的表单(以线程安全的方式)(除了BaseWindow)并打开一个新的LoginScreen表单。目前所有的形式都被关闭,但当新的LoginScreen形式开始时,它的UI被冻结。

代码如下:

{
public static Form[] OpenForms
{
get
{
Form[] forms = null;
int count = Application.OpenForms.Count;
forms = new Form[count];
if (count > 0)
{
int index = 0;
foreach (Form form in Application.OpenForms)
{
forms[index++] = form;
}
}
return forms;
}
}

delegate void CloseMethod(Form form);
static private void CloseForm(Form form)
{
if (!form.IsDisposed)
{
if (form.InvokeRequired)
{
CloseMethod method = new CloseMethod(CloseForm);
form.Invoke(method, new object[] { form });
}
else
{
form.Close();
}
}
}
static public void CloseAllForms()
{
// get array because collection changes as we close forms
Form[] forms = OpenForms;
// close every open form
foreach (Form form in forms)
{
if (form.GetType() != typeof(BaseWindow))
{
CloseForm(form);
}
}          
}
private void PollUpdates(object sender, EventArgs e)
{
;
if (Program.Globalsession.SessionId != default(Guid))
{
if (DateTime.Compare(Program.Globalsession.ValidTo, DateTime.Now) < 0)
{
CloseAllForms();
var login = new LoginScreen();
login.Show();
}
}
}     
public BaseWindow()
{
InitializeComponent();
var login = new LoginScreen();
login.Show();
var theTimer = new System.Timers.Timer(5000);
theTimer.Elapsed += PollUpdates;
theTimer.Start();
}
}

你知道我做错了什么吗?

我猜你使用了错误的计时器类型。使用System.Windows.Forms.Timer(而不是System.Timers.Timer)。细节。

所以你是在一个线程上创建LoginScreen而不是UI线程。

相关内容

  • 没有找到相关文章

最新更新