当使用Debug和Release二进制文件时,VS应用程序在Win7中停止

时间:2014-05-20 15:50:47

标签: vb.net winforms visual-studio-2010 visual-studio

所以,我在Visual Basic中做了一个迭代的Towers of Hanoi算法,它在while循环中运行(VB中的递归速度很慢)。问题是它编译了okey,它甚至在通过Visual Studio启动时运行,但是当通过Debug和Release生成执行时,动画停止,并显示以下消息:

enter image description here

过了一会儿,我只看到所有碎片移动到目的地极点,消息消失了。所以它不是一个崩溃的说法,因为应用程序仍然在后台运行,它只是这个消息弹出,毁了动画。我只是希望我的程序在直接从Visual Studio启动时运行。

经过一番思考...... 我开始相信这种情况会发生,因为Win7会将应用程序在while循环中运行的事实视为无响应(在河内的塔中需要花费一些时间重新排列),因此它会尝试关闭它。

如何让我的应用程序忽略Window的广告?

1 个答案:

答案 0 :(得分:0)

我建议您在应用程序空闲事件中进行计算,就像创建Windows游戏时一样。这样可以确保不阻止消息队列。

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
        AddHandler Application.Idle, AddressOf Me.OnApplicationIdle
    End Sub

    Private Sub OnApplicationIdle(sender As Object, e As EventArgs)
        Static rnd As New Random()
        Dim message As MSG = Nothing
        Do While (Not PeekMessage(message, IntPtr.Zero, 0, 0, 0))
            '...
            Me.BackColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))
            '...
        Loop
    End Sub

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Friend Shared Function PeekMessage(<[In](), Out()> ByRef msg As MSG, ByVal hwnd As IntPtr, ByVal msgMin As Integer, ByVal msgMax As Integer, ByVal remove As Integer) As Boolean
    End Function

    <StructLayout(LayoutKind.Sequential)> _
    Friend Structure MSG
        Public hwnd As IntPtr
        Public message As Integer
        Public wParam As IntPtr
        Public lParam As IntPtr
        Public time As Integer
        Public pt_x As Integer
        Public pt_y As Integer
    End Structure

End Class