2016-08-27 2 views
0

임의의 파일을 열고 순차적으로 읽는이 루프가 있습니다. 이 파일들은 문자 그대로 수천 가지가 될 수 있기 때문에 검색 중지 버튼을 클릭 할 때 시스템이 정지하지 않고 for 및 exit 루프를 종료하는 방법이 필요합니다.정상적으로 루프를 종료하는 방법 .net VB

현재 가장 좋은 방법은 검색 중지 버튼을 클릭 할 때 x 루프를 마지막 숫자로 설정하는 것입니다.

종료를위한 루프와 종료를 모두 종료하려고 시도했지만 시스템이 루프를 중지하기 전에 시스템이 잠시 멈춘 이유는 이들과 아무런 차이가 없습니다. 프로그램이 응답하기 전에 검색 중지 버튼을 몇 번 클릭해야합니다. 내가 스레드에 내 코드를 동봉 만약 내가 완벽하게 루프 (스레드를 중단을 막을 수 있다는 것을 발견 몇 가지 추가 연구를 수행 한 후

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
    Button1.Click 

    stopsearch = False 

    Dim x As Single 

    Dim startvalue As Single 
    Dim endvalue As Single 


    startvalue = Val(TextBox1.Text) 
    endvalue = Val(TextBox2.Text) 



    If TextBox4.Text <> "" Then 


     ListView1.Items.Clear() 

     ProgressBar1.Minimum = startvalue 
     ProgressBar1.Maximum = endvalue 

     Button1.Text = "Searching . . . " 

     For x = startvalue To endvalue 
      ProgressBar1.Value = x 
      Dim f As String 

      If stopsearch = True Then 
       x = endvalue 
        exit for 
      End If 


      f = TextBox3.Text + "-" + Trim(Str(x)) + "." + company 

      If File.Exists(D_Drive + "\STORE-16\" + f) Then 

       'MsgBox(f) 

       FileClose(30) 
       FileOpen(30, D_Drive + "\store-16\" + f, OpenMode.Input) 


       Try 
        Dim sr As String = "" 
        Do While Not EOF(30) 
         sr = LineInput(30) 


         If InStr(UCase(sr), UCase(TextBox4.Text)) > 0 Then 

          Dim i As ListViewItem = ListView1.Items.Add(Str(x)) 
          i.SubItems.Add(sr) 
          Application.DoEvents() 

         End If 
        Loop 
       Finally 
        FileClose(30) 
       End Try 

      End If 


     Next x 

     Button1.Text = "Re-start Search " 

    End If 
End Sub 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    stopsearch = True 
End Sub 
+0

'시스템이 루프를 멈출 수 있기 전에 시스템이 잠깐 멈춘 이유'DoEvents'를 종료하고 루프의 진행률 막대를 업데이트하지 마십시오.하지만 다른 코드가 아닙니다. 리뷰. 루프를 빠져 나가려면 'Exit For'를 호출하십시오 ... 변수를 설정하면 루프가 종료되므로 그 루프가 종료됩니다. – Codexer

+0

다른 노트'vb6 <> vb.net' ... 코드에서 많은 것을 참조합니다. 그것을 밖으로 추출 및 최신 .net 함수 및 메서드를 사용하는 것이 좋습니다 ... – Codexer

+0

Zaggler 주석 주셔서 감사합니다 - 나에 대한 끝내기 위해 삽입 한 진행률 표시 줄을 업데이트하지 않으려 고 노력했다. . . vb6 코드를 사용하는 것 자체가 도움이되지 않습니다. 누군가가 내 .net 함수와 메소드를 사용하여 코드를 변형 할 수 있도록 학습 할 수 있다면. . 구현을 시작하려면이 코드를 볼 때 실마리가 없습니다. . . –

답변

-1

- : 다음의 코드의 일부는 루프를 수행).

내가 가지고있는 방식대로 스레드를 사용한다면;

Private Sub LaybuyInputDialog_Load(sender As Object, e As EventArgs) Handles Me.Load 

     Me.CheckForIllegalCrossThreadCalls = False 

    End Sub 

또한 폼의 상단에 Imports.System.Threading를 삽입하는 것을 잊지 마세요 -이 : 그것은 형태의 로딩에 다음과 같은 코드를하는 것이 바람직합니다. 아래 스레딩에 새로운 사람을 위해서

내가 변경 한 코드입니다 : -
Dim thread As System.Threading.Thread 



    Private Sub DoSearch() 

     Button1.Hide() 

     stopsearch = False 

     Dim x As Single 

     Dim startvalue As Single 
     Dim endvalue As Single 


     startvalue = Val(TextBox1.Text) 
     endvalue = Val(TextBox2.Text) 



     If TextBox4.Text <> "" Then 


      ListView1.Items.Clear() 

      ProgressBar1.Minimum = startvalue 
      ProgressBar1.Maximum = endvalue 

      Button1.Text = "Searching . . . " 

      For x = startvalue To endvalue 
       ProgressBar1.Value = x 
       Dim f As String 

       If stopsearch = True Then 
        x = endvalue 
        Exit For 
       End If 



       f = TextBox3.Text + "-" + Trim(Str(x)) + "." + company 

       If File.Exists(D_Drive + "\STORE-16\" + f) Then 

        'MsgBox(f) 

        FileClose(30) 
        FileOpen(30, D_Drive + "\store-16\" + f, OpenMode.Input) 


        Try 
         Dim sr As String = "" 
         Do While Not EOF(30) 
          sr = LineInput(30) 

          If stopsearch = True Then 
           x = endvalue 
           Exit Do 
          End If 

          If InStr(UCase(sr), UCase(TextBox4.Text)) > 0 Then 

           Dim i As ListViewItem = ListView1.Items.Add(Str(x)) 
           i.SubItems.Add(sr) 
           If ListView1.Items.Count > 0 Then 
            ListView1.Items(ListView1.Items.Count - 1).EnsureVisible() 
           End If 
           Application.DoEvents() 

          End If 
         Loop 
        Finally 
         FileClose(30) 
        End Try 

       End If 


      Next x 

      Button1.Text = "Re-start Search " 

     End If 
    End Sub 


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     thread = New System.Threading.Thread(AddressOf DoSearch) 
     thread.Start() 


    End Sub 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
     stopsearch = True 
     Try 
      thread.Suspend() 
     Catch ex As Exception 
      ' 
     End Try 

    End Sub 

이 희망

내 대답은 사람이하는 데 도움이; 더 나은 해결책이 있다면 언제든지 말씀해주십시오! !

관련 문제