0

好吧,我有一些潜艇,例如:

Private Sub somesub() '在 1 个半小时内达到 900 mb 的进程 End Sub

我想重新启动应用程序,释放内存,然后返回到原来的位置。

确切地说,我有一个添加联系人的应用程序,当添加 2000 个联系人时,它会达到 900mb...

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

我能做些什么?谢谢!

4

1 回答 1

1

我在下面编写了一些代码,可以解决您的问题。它当然应该将您的位置保存到文件中,然后当文件再次运行时,它会重新加载该位置。

几点。

  1. 我移动了您对 pUser 的声明,并在完成后将其设置为空。通过这种方式,该对象被标记为立即处置。结构变化可能会导致超过 200 转。但它可能会慢一点。

  2. 您将需要为您的列表框进行某种重新加载。为简洁起见,我假设您没有将其作为样本的一部分。

  3. 我将您的 foreach 循环更改为 for 循环结构。这允许您跟踪您在列表中的位置.. 结果我必须创建一个新的 oUser,因为您在 foreach 中迭代它并且没有列出它的类型,您需要修复那部分代码。

  4. 显然我没有编译下面的代码,但它应该给你一个体面的开始,让你尝试做什么。

  5. 请注意 Process.Start,因为您可以将当前进程设置为启动另一个进程并等待该进程退出,然后再退出当前进程,这将非常非常非常糟糕,并且真的会很快导致 OutOfMemoryException。您需要让当前进程启动下一个实例,然后不检查它是否成功启动它..退出。或者,如果您在评论中使用了重启命令,请使用它。进程生成方法可能会更有效地执行您想要的操作,因为您在计算机上启动了一个新进程并让旧进程被垃圾收集(从而释放它正在使用的资源。

    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
    
于 2013-07-25T22:15:35.187 回答