我有一个 ac# 项目,其中一个表单在一些后台任务完成后隐藏自己,在命令时打开,但在某些条件下,调用 this.Show() 或将 this.Visible 设置为 true 不会使表单再现!我什至可以确定表单的 Visible 值输出的状态,并且表单似乎认为它在屏幕上,但它无处可见。
似乎导致它中断的条件是,如果我在窗体隐藏之前给出另一个窗口控件。如果我让它在工作时留在前面,隐藏自己,然后告诉它回来它总是如此,即使我事后改变焦点,但我事先改变焦点,它不会重新出现,即使形式。显示似乎被调用。
有没有人知道为什么会发生这种情况?这是一个如此奇怪的情况,尤其是因为 form.Visible 的状态发生了变化。
public partial class testForm : Form {
private void testForm_Sometrigger(object sender, EventArgs e) {
//some delay. In the actual program, this is thanks to a background worker working.
Thread.Sleep(5000);
//manually change focus to another process/window before this
this.Hide();
//I've been adding a wait here, since in practice the form won't be called again right away.
Thread.Sleep(3000);
//show form again.
this.Show();
// this will be true even if the form isn't actually visible
Console.WriteLine("is visible? "+this.Visible.ToString());
}
}
应要求添加代码块。这一点没什么,只是展示和隐藏和时间流逝,真的。
原来没有出现的窗口可能与表单不活动和 UI 线程没有返回它有关,因此在 show 调用后添加一个 Form.Activate() 修复了它。
原始问题已更新解决方案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句