2013-07-25 5 views
0

잘 나는 같은 일부 잠수정이 1 시간 하위 절반 끝 900 메가 바이트에 도달자동 재시작 한 후 VB.NET에서 하위를 계속

개인 서브 somesub() 'Processses을

앱을 다시 시작하고 메모리를 삭제 한 다음 원래 위치로 돌아가고 싶습니다.

정확히 연락처를 추가하는 앱이 있습니다. 연락처가 2000 개 추가 될 때 900 메가 바이트에 도달합니다 ... 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 

어떻게해야합니까? 감사!

+0

가능한 중복을 [ 단일 인스턴스 응용 프로그램 다시 시작] (http://stackoverflow.com/questions/745447/restarting-a-single-instance-application) – ElektroStudios

답변

1

아래에서 문제를 해결할 수있는 몇 가지 코드를 작성했습니다. 확실히 파일에 대한 위치를 저장해야하고 파일이 다시 실행되면 해당 위치가 다시로드됩니다.

몇 점.

  1. 나는 pUser 선언을 옮기고 끝나면 아무것도 설정하지 않았습니다. 이 방법으로 해당 물체는 즉시 처분 대상으로 표시됩니다.이 구조 변경으로 200 회 이상의 회전이 발생할 수 있지만 속도가 느려질 수 있습니다.

  2. 목록 상자에 어떤 종류의 재로드가 필요할 것입니다. 내가 간략하게 샘플의 일부로 포함시키지 않았다고 가정합니다.

  3. foreach 루프를 for 루프 구조로 변경했습니다. 이렇게하면 목록 내의 위치를 ​​추적 할 수 있습니다. 결과적으로 foreach에서 iterating하고 해당 유형을 나열하지 않았으므로 코드의 해당 부분을 수정해야하므로 새 ouser를 작성해야했습니다.

  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 
    
관련 문제