自动重启,然后在VB.NET中继续Sub

时间:2013-07-25 21:40:22

标签: vb.net restart skype4com

我有一些像:

Private Sub somesub() '过程在1小时半内达到900 mb 结束子

我想重新启动应用程序,处置内存然后返回到我所在的位置。

确切地说,我有一个添加联系人的应用程序,当添加2000个联系人时它达到900mb ...我想停止每200个联系人,并且我已经说过了,并且我没有测试的代码:< / p>

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

我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:1)

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

有几点。

  1. 我移动了你的pUser声明,并在我完成时将其设置为空。通过这种方式,对象被标记为立即处理..在结构发生变化的情况下,你可能会获得超过200次旋转......但它可能会慢一些。

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

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

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

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

    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