Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 启动窗体.close和应用程序.exit未终止应用程序_.net_Vb.net_Winforms - Fatal编程技术网

.net 启动窗体.close和应用程序.exit未终止应用程序

.net 启动窗体.close和应用程序.exit未终止应用程序,.net,vb.net,winforms,.net,Vb.net,Winforms,我在应用程序启动窗体的.load偶数处理程序中有以下循环: While (Not Directory.Exists(My.Settings.pathToHome)) Dim response As MessageBoxResult = Forms.MessageBox.Show("Some message","Some Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)

我在应用程序启动窗体的.load偶数处理程序中有以下循环:

        While (Not Directory.Exists(My.Settings.pathToHome))
        Dim response As MessageBoxResult = Forms.MessageBox.Show("Some message","Some Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
        If response = MessageBoxResult.Cancel Then
            Me.Close() 'can comment this line or next or leave both uncommented
            Forms.Application.Exit()
        End If
        options.ShowDialog() 'this line can be commented
    End While
如果用户在messagebox上选择“cancel”,则可以执行Me.Close()和Forms.Application.Exit()两行中的一行或两行,但不是终止应用程序,而是执行while循环。通过单步执行调试器可以明确地看到这一点

在消息框上第一次取消后,选项窗体和消息框永远不会打开,尽管在循环旋转时可能会看到其中一个或两个选项“闪烁”。如果单步执行调试器,我会听到messagebox中的“蜂鸣”,尽管它没有出现


我想我也可以在里面加一个“出口接头”。但这是否必要,是否可靠?看起来很奇怪,尤其是application.exit没有成功终止线程。

表单.application.exit之后,您需要
退出


Application.Exit
仍然允许线程完成它们正在做的事情,而且由于您处于一个有效的无限循环中,因此它实际上永远不会让应用程序关闭。

您需要在
表单.Application.Exit之后退出


Application.Exit
仍然允许线程完成它们正在做的事情,而且由于您实际上处于无限循环中,它永远不会让应用程序关闭。

Application.Exit()
只是告诉Windows您的应用程序要退出,并清理一些资源。它不会停止您的代码,因此您的循环会继续运行

您负责让所有线程停止。您确实可以通过添加
退出子项
来实现这一点。或者一些
环境
方法,比如Or,但这两种方法都是多余的,在您的情况下,您只能使用它们来隐藏糟糕的设计。只需退出循环。

Application.exit()
只是告诉Windows您的应用程序要退出,并清理一些资源。它不会停止您的代码,因此您的循环会继续运行


您负责让所有线程停止。您确实可以通过添加
退出子项
来实现这一点。或者一些
环境
方法,比如Or,但这两种方法都是多余的,在您的情况下,您只能使用它们来隐藏糟糕的设计。只要退出循环。

你只需要打破循环。正如其他答案所涵盖的那样,Application.Exit()会发出关闭应用程序的信号,但它会让所有应用程序线程完成它们正在做的事情,因此当您保持在该循环中时,您的主线程将永远不会完成

打破这一循环的几种方法;最合适的方法是退出while语句,或者退出整个函数

While (Not Directory.Exists(My.Settings.pathToHome))
    Dim response As MessageBoxResult = Forms.MessageBox.Show("Some message","Some Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    If response = MessageBoxResult.Cancel Then
        Me.Close() 'can comment this line or next or leave both uncommented
        Forms.Application.Exit()
        Exit While 'or Exit Sub
    End If
    options.ShowDialog() 'this line can be commented
End While

你只需要打破这个循环。正如其他答案所涵盖的那样,Application.Exit()会发出关闭应用程序的信号,但它会让所有应用程序线程完成它们正在做的事情,因此当您保持在该循环中时,您的主线程将永远不会完成

打破这一循环的几种方法;最合适的方法是退出while语句,或者退出整个函数

While (Not Directory.Exists(My.Settings.pathToHome))
    Dim response As MessageBoxResult = Forms.MessageBox.Show("Some message","Some Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)
    If response = MessageBoxResult.Cancel Then
        Me.Close() 'can comment this line or next or leave both uncommented
        Forms.Application.Exit()
        Exit While 'or Exit Sub
    End If
    options.ShowDialog() 'this line can be commented
End While

这种行为是故意的。阅读文档。这种行为是故意的。阅读文档。