Main() 中的代码可访问或不可访问取决于打开另一个表单
我有一些袖珍电脑应用程序,我遇到了一个严重的问题,如下所述:https://stackoverflow.com/questions/472598< /a> . 在寻找解决方案时,我在 Main(): 中尝试了一些实际上非常愚蠢的代码:
[MTAThread]
static void Main()
{
Application.Run(new Tasks());
Application.Exit();
}
并在退出时设置断点。 如果我只是运行应用程序,然后关闭窗口,就会到达断点。 如果我运行应用程序,然后打开另一个窗口:
private void questButton_Click(object sender, EventArgs e)
{
QuestionnairesWindow questWindow = new QuestionnairesWindow();
questWindow.Show();
this.Hide();
}
然后从它返回到初始窗口:
private void backButton_Click(object sender, EventArgs e)
{
Tasks tasksWindow = new Tasks();
tasksWindow.Show();
this.Close();
}
并以与第一次相同的方式关闭初始窗口,则永远不会到达 Apllication.exit() 代码,我的印象是该应用程序并未真正关闭(我无法再次打开它)。 抱歉,如果描述很复杂
编辑:问题是 - 有什么想法为什么它的行为不同吗?
I've some pocket pc app and i'm having a serious problem with it described here: https://stackoverflow.com/questions/472598 . While looking for a solution i tried some actually quite stupid code in Main():
[MTAThread]
static void Main()
{
Application.Run(new Tasks());
Application.Exit();
}
and set breakpoint on exit. if i just run the application and then close the window the breakpoint is reached. if i run the application and then open another window:
private void questButton_Click(object sender, EventArgs e)
{
QuestionnairesWindow questWindow = new QuestionnairesWindow();
questWindow.Show();
this.Hide();
}
and then get back from it to initial window:
private void backButton_Click(object sender, EventArgs e)
{
Tasks tasksWindow = new Tasks();
tasksWindow.Show();
this.Close();
}
and close the initial one the same way as the first time, the Apllication.exit() code is never reached and i have an impression that the application isn't really closed ( i can't open it again). sorry if the description is complicated
edit: the question is - any ideas why is it behaving differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Main()
中的new Tasks()
与TaskstasksWindow = new Tasks();
不是同一个对象,您有 2 个 Tasks 对象,所以结束第二个,第一个仍然存在并且永远不会消失。 您需要将当前
任务
的引用传递给QuestionnairesWindow
。您可以使用附加的
QuestionnairesWindow
构造函数来做到这一点:使用:
new Tasks()
inMain()
is not the same object withTasks tasksWindow = new Tasks();
You got 2 objects of Tasks, so closing second, first is still present and never dies. You need to pass to
QuestionnairesWindow
the reference of currentTasks
.You can do that with additional
QuestionnairesWindow
constructor:using:
您遇到的问题是,在 backButton_Click 中您正在创建任务窗口的新实例。 原来的任务窗口仍在运行,只是被隐藏了。 我怀疑您需要将对原始任务表单的引用传递到 QuestionnairesWindow 中,以便它可以选择再次显示它。
您需要在 QuestionnairesWindow 上添加公共/内部方法,在其中设置哪个任务表单导致 QuestionnairesWindow 打开。 在调用 this.Hide() 之前,您可以使用 this 引用在 questButton_Click 中调用此方法。 该引用将存储在 QuestionnairesWindow 内的私有变量中。
然后在 backButton_Click 中,使用这个私有变量来调用 .Show() 方法,以显示父窗体。
The issue you have is that in backButton_Click you are creating a new instance of the Tasks window. The original Tasks window is still running, it is just hidden. I suspect that you need to pass a reference to the original Tasks form into the QuestionnairesWindow so it can choose to show it again.
You either need to add a public/internal method on your QuestionnairesWindow where you set which Tasks form caused the QuestionnairesWindow to open. You call this method in questButton_Click with the this reference prior to calling this.Hide(). This reference will be stored in a private variable within the QuestionnairesWindow.
Then in the backButton_Click, you use this private variable, for calling the .Show() method, to show the parent form.
这一行似乎是问题所在:
如果我正确理解您的代码,那么在您的 backButton_Click 中,您正在创建任务表单的一个新实例,而不是显示您最初隐藏的实例。 这意味着应用程序永远不会终止,因为仍然有一个窗口打开,但隐藏。
您可能想要做的是将 Tasks 表单引用传递给 QuestionnairesWindow 表单。
This line seems to be the problem:
If I understand your code correctly, in your backButton_Click, you're creating a new instance of your Tasks form, isntead of showing the one you originally hid. This means that the application never terminates, because there is still one window open, but hidden.
What you probably want to do is pass the Tasks form reference to the QuestionnairesWindow form.