自动重启,然后在 VB.NET 中继续 Sub [英] Auto-restart and then continue the Sub in VB.NET
问题描述
好吧,我有一些像:
私有子 somesub()'在 1 个半小时内达到 900 mb 的进程结束子
我想重新启动应用程序,处理内存,然后回到原来的位置.
没错,我有一个添加联系人的应用程序,当添加 2000 个联系人时它会达到 900mb...我想每 200 个联系人停止一次,我已经说过了,我没有测试过的代码:
导入 SKYPE4COMLib公共类 frmMainDim pUser 为 SKYPE4COMLib.UserDim contactos 作为整数如果接触<200 那么对于 ListBox1.Items 中的每个 oUserpUser = oSkype.User(oUser)pUser.BuddyStatus = SKYPE4COMLib.TBuddyStatus.budPendingAuthorizationoSkype.Friends.Add(pUser)联系人 += 1下一个别的'System.Windows.Forms.Application.Restart()'我需要一个代码,继续我所在的地方,在这里......万一结束子结束类
我能做什么?谢谢!
我在下面编写了一些代码,可以解决您的问题.它当然应该将您的位置保存到一个文件中,然后当文件再次运行时,它会重新加载该位置.
几点.
我移动了您对 pUser 的声明,并在完成后将其设置为空.通过这种方式,对象会立即被标记为待处理......随着结构的变化,你可能会从中获得 200 多转......但它可能会慢一点.
您需要为列表框重新加载某种形式.为简洁起见,我假设您没有将其作为样本的一部分.
我将您的 foreach 循环更改为 for 循环结构.这允许您跟踪您在列表中的位置.. 结果我不得不创建一个新的 oUser,因为您在 foreach 中迭代它并且没有列出它的类型,您需要修复那部分代码.>
显然我还没有编译下面的代码,但它应该让你开始尝试做的事情.
注意 Process.Start,因为您可以设置当前进程启动另一个进程并等待该进程退出,然后再退出当前进程,这将非常非常非常糟糕,并且真的会导致OutOfMemoryException 很快.您需要让当前进程启动下一个实例,然后不检查它是否在启动时成功.. 退出.或者,如果您在评论中使用了重新启动命令,请使用它.进程生成方法可能会更有效地执行您想要的操作,因为您在计算机上启动一个新进程并让旧进程被垃圾收集(从而释放它正在使用的资源.
导入 SKYPE4COMLib公共类 frmMain'将代码放在这里以从文件中加载位置将起始位置调暗为整数 = 0如果 IO.File.Exists("c:\filename.txt")使用 sr 作为新 IO.StreamReader("c:\filename.txt")读先生起始位置 = Convert.ToInteger(sr.ReadToEnd)sr.关闭结束使用万一'可能需要一些代码来重新加载你的列表框Dim contactos 作为整数Dim CurrentPosition 作为整数 = 0如果接触<200 和起始位置
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!
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.
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.
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.
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.
Obviously I have not compiled the below code, but it should give you a decent start on what your trying to do.
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
这篇关于自动重启,然后在 VB.NET 中继续 Sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!