Auto-restart and then continue the Sub in VB.NET

时光总嘲笑我的痴心妄想 提交于 2019-12-11 16:30:01

问题


Well I have some subs like:

Private Sub somesub() 'Processses that reach 900 mb in 1 hour and an half End Sub

I want to restart the app, dispose memory and then return to where I was.

Exactly, I have an app that adds contacts and well It reach 900mb when 2000 contacts are added... I want to stop every 200 contacts, and do that I have said, and the code that I have no tested:

Imports SKYPE4COMLib

Public Class frmMain

Dim pUser As SKYPE4COMLib.User
        Dim contactos As Integer

        If contactos < 200 Then
            For Each oUser In ListBox1.Items

                pUser = oSkype.User(oUser)
                pUser.BuddyStatus = SKYPE4COMLib.TBuddyStatus.budPendingAuthorization
                oSkype.Friends.Add(pUser)
                contactos += 1
            Next
        Else
            'System.Windows.Forms.Application.Restart()
            'I need a code that continues where I was, here...
        End If
End Sub

End Class

What can I do? Thanks!


回答1:


I have written some code below that may solve your problem. It certainly should save your position to a file and then when the file runs again, it would reload that position.

A couple of points.

  1. I moved your declaration of pUser, and set it to nothing when I was done. This way the object is marked for disposal immediately.. you might get more than 200 revolutions out of this with that structure change.. but it might be a tad slower.

  2. You will need some sort of reload for your listbox. I assume that you did not include it as part of your sample for brevity.

  3. I changed your foreach loop to a for loop structure. This allows you to track your position within the list .. as a result I had to create a new oUser since you were iterating that in the foreach and did not list its type, you will need to fix that part of the code.

  4. Obviously I have not compiled the below code, but it should give you a decent start on what your trying to do.

  5. Be careful of the Process.Start, as you can set the current process to start another process and wait for that process to exit before exiting the current one and that would be very very very very bad and really cause an OutOfMemoryException quickly. You need to let the current process start the next instance and then without checking to see if it was successful at starting it .. exit. Or if you have used the restart command in your comments use that. The process spawning method might do what your wanting more effectively because your starting a new process on the computer and letting the old one be garbage collected (thus releasing the resources it was using.

    Imports SKYPE4COMLib
    
    Public Class frmMain
    
            'Put code here to load position from the file
            Dim startingPosition as Integer = 0
            If IO.File.Exists("c:\filename.txt")
                Using sr as New IO.StreamReader("c:\filename.txt")
                    sr.Read
                    StartingPosition = Convert.ToInteger(sr.ReadToEnd)
                    sr.Close
                End Using
            End If
            'Probably needs some code somewhere to reload your listbox
            Dim contactos As Integer
            Dim CurrentPosition as Integer = 0
            If contactos < 200 and StartingPosition < ListBox1.Items.Count Then
                For x as integer = StartingPosition to ListBox1.Items.Count - 1
                    Dim oUser as <YOURTYPEHERE> = Ctype(ListBox1.Items(x), <YOURTYPEHERE>)
                    Dim pUser As SKYPE4COMLib.User
                    pUser = oSkype.User(oUser)
                    pUser.BuddyStatus = SKYPE4COMLib.TBuddyStatus.budPendingAuthorization
                    oSkype.Friends.Add(pUser)
                    contactos += 1
                    pUser = Nothing  'set the garbage collection to collect this.
                    CurrentPosition = x
                Next
            Else
                'Save Your place to an external File, all your doing here is opening a file
                'and saving the index of where you are in the listbox.
                Using sw as New IO.StreamWriter("c:\filename.txt")
                     sw.Write(CurrentPosition)
                     sw.Close
                End Using
                'use the process namespace to have this app start the next copy of your app
                'be careful not to use WaitForExit or you will have two copies in memory...
                Process.Start("exename")
                'or if the command below exists.. use that.. I never have.
                'System.Windows.Forms.Application.Restart()
                'I need a code that continues where I was, here...
            End If
        End Sub
    End Class
    


来源:https://stackoverflow.com/questions/17869249/auto-restart-and-then-continue-the-sub-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!