当前位置:编程学习 > VB >>

vb.net问题

Me.Dispose和Me.Close和End有什么区别?哪个资源释放的更彻底?
追问:Environment.Exit(0)中的退出代码"0"有什么用吗?都有哪些值?
答案:执行 dispose 后,由.net 的GC垃圾回收器周期性地回收资源, me.close 方法的基类会调用 dispose 方法

end 是从vb中继承下来的语句,直接指示退出程序.程序的资源将被释放,程序并未希望结束时,不适合使用该方法.若要强行退出应用程序,在.net中可以使用Environment.Exit(0)

 

 下面是 system.windows.forms.form 类对 close 和 dispose 的封装:

'------- Close

Public Sub Close()
    If MyBase.GetState(&H40000) Then
      Throw New InvalidOperationException(SR.GetString("ClosingWhileCreatingHandle", New Object() { "Close" }))
     End If
    If MyBase.IsHandleCreated Then
      Me.closeReason = CloseReason.UserClosing
      MyBase.SendMessage(&H10, 0, 0)
    Else
      MyBase.Dispose
    End If
End Sub 

 

'------- Dispose

 Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
        Me.CalledOnLoad = False
        Me.CalledMakeVisible = False
        Me.CalledCreateControl = False
        If MyBase.Properties.ContainsObject(Form.PropAcceptButton) Then
            MyBase.Properties.SetObject(Form.PropAcceptButton, Nothing)
        End If
        If MyBase.Properties.ContainsObject(Form.PropCancelButton) Then
            MyBase.Properties.SetObject(Form.PropCancelButton, Nothing)
        End If
        If MyBase.Properties.ContainsObject(Form.PropDefaultButton) Then
            MyBase.Properties.SetObject(Form.PropDefaultButton, Nothing)
        End If
        If MyBase.Properties.ContainsObject(Form.PropActiveMdiChild) Then
            MyBase.Properties.SetObject(Form.PropActiveMdiChild, Nothing)
        End If
        If (Not Me.MdiWindowListStrip Is Nothing) Then
            Me.MdiWindowListStrip.Dispose
            Me.MdiWindowListStrip = Nothing
        End If
        If (Not Me.MdiControlStrip Is Nothing) Then
            Me.MdiControlStrip.Dispose
            Me.MdiControlStrip = Nothing
        End If
        If (Not Me.MainMenuStrip Is Nothing) Then
            Me.MainMenuStrip = Nothing
        End If
        Dim form As Form = DirectCast(MyBase.Properties.GetObject(Form.PropOwner), Form)
        If (Not form Is Nothing) Then
            form.RemoveOwnedForm(Me)
            MyBase.Properties.SetObject(Form.PropOwner, Nothing)
        End If
        Dim formArray As Form() = DirectCast(MyBase.Properties.GetObject(Form.PropOwnedForms), Form())
        Dim i As Integer = (MyBase.Properties.GetInteger(Form.PropOwnedFormsCount) - 1)
        Do While (i >= 0)
            If (Not formArray(i) Is Nothing) Then
                formArray(i).Dispose
            End If
            i -= 1
        Loop
        If (Not Me.smallIcon Is Nothing) Then
            Me.smallIcon.Dispose
            Me.smallIcon = Nothing
        End If
        Me.ResetSecurityTip(False)
        MyBase.Dispose(disposing)
        Me.ctlClient = Nothing
        Dim menu As MainMenu = Me.Menu
        If ((Not menu Is Nothing) AndAlso (menu.ownerForm Is Me)) Then
            menu.Dispose
            MyBase.Properties.SetObject(Form.PropMainMenu, Nothing)
        End If
        If (Not MyBase.Properties.GetObject(Form.PropCurMenu) Is Nothing) Then
            MyBase.Properties.SetObject(Form.PropCurMenu, Nothing)
        End If
        Me.MenuChanged(0, Nothing)
        Dim menu2 As MainMenu = DirectCast(MyBase.Properties.GetObject(Form.PropDummyMenu), MainMenu)
        If (Not menu2 Is Nothing) Then
            menu2.Dispose
            MyBase.Properties.SetObject(Form.PropDummyMenu, Nothing)
        End If
        Dim menu3 As MainMenu = DirectCast(MyBase.Properties.GetObject(Form.PropMergedMenu), MainMenu)
        If (Not menu3 Is Nothing) Then
            If ((menu3.ownerForm Is Me) OrElse (menu3.form Is Nothing)) Then
                menu3.Dispose
            End If
            MyBase.Properties.SetObject(Form.PropMergedMenu, Nothing)
        End If
    Else
        MyBase.Dispose(disposing)
    End If
End Sub

me.dispose这个是最彻底  释放资源

 

Me.Close  这个是关闭

end  只是结束 一般用在程序的语句中 如结束语句

me.close 会调用窗口的close方法,在close方法中系统会自动dispose

所以close在某些情况下和dispose效果一样

end会使VB程序直接结束

以下摘自MSDN

End 语句可以放在过程中的任何位置,以强制整个应用程序停止运行。End 关闭通过 Open 语句打开的所有文件,并清除应用程序的所有变量。只要其他程序没有引用应用程序的对象并且应用程序的代码当前都未运行,该应用程序就会立即关闭。

上一个:VB怎么弄注册表?
下一个:vb文件读取问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,