自动重新启动,然后在VB.NET中继续子进程

自动重新启动,然后在VB.NET中继续子进程,vb.net,restart,skype4com,Vb.net,Restart,Skype4com,我有一些潜水艇,比如: 私人Sub somesub() '在1个半小时内达到900 mb的进程 端接头 我想重新启动应用程序,释放内存,然后返回到原来的位置 没错,我有一个添加联系人的应用程序,当添加2000个联系人时,它会达到900mb。。。我想每200个联系人停止一次,并按照我说过的那样做,我没有测试过的代码: Imports SKYPE4COMLib Public Class frmMain Dim pUser As SKYPE4COMLib.User Dim cont

我有一些潜水艇,比如:

私人Sub somesub() '在1个半小时内达到900 mb的进程 端接头

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

没错,我有一个添加联系人的应用程序,当添加2000个联系人时,它会达到900mb。。。我想每200个联系人停止一次,并按照我说过的那样做,我没有测试过的代码:

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
导入SKYPE4COMLib
公开课
Dim pUser作为SKYPE4COMLib.User
Dim contactos作为整数
如果contactos<200,则
对于ListBox1.项中的每个oUser
pUser=oSkype.User(User)
pUser.BuddyStatus=SKYPE4COMLib.TBuddyStatus.buddingAuthorization
oSkype.Friends.Add(pUser)
contactos+=1
下一个
其他的
'System.Windows.Forms.Application.Restart()
“我需要一个代码继续我在这里的位置。。。
如果结束
端接头
末级

我能做什么?谢谢

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

几点

  • 我移动了你的pUser声明,当我完成时,将其设置为零。通过这种方式,该物体被标记为立即处置。。通过这种结构上的改变,你可能会得到超过200次的转数。。但可能会慢一点

  • 您需要对列表框进行某种重新加载。为了简洁起见,我假设您没有将其作为示例的一部分

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

  • 显然,我还没有编译下面的代码,但它应该给你一个体面的开始,你试图做什么

  • 小心进程。启动,因为您可以将当前进程设置为启动另一个进程,并在退出当前进程之前等待该进程退出,这将非常糟糕,并且会很快导致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
    
    导入SKYPE4COMLib
    公开课
    '将代码放在此处以从文件加载位置
    Dim startingPosition为整数=0
    如果IO.File.Exists(“c:\filename.txt”)
    使用sr作为新的IO.StreamReader(“c:\filename.txt”)
    高级雷德
    StartingPosition=转换为Integer(sr.ReadToEnd)
    高级关闭
    终端使用
    如果结束
    '可能需要一些代码来重新加载列表框
    Dim contactos作为整数
    Dim CurrentPosition作为整数=0
    如果contactos<200且StartingPosition
  • 可能重复的