2015-10-19 142 views
0
申請凍結

我用下面的代碼獲取文件大小的目錄裏面 並把它在Label1「對於每一個」循環:在Vb.net

For Each foundFile As String In My.Computer.FileSystem.GetFiles(_ 
    "\windows",Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly,_ 
    "*.*") 
     Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile) 
     Label1.Text = Label1.Text + filesizelabel.Length 
    Next 

的問題是,我有每個循環超過50個(系統清理應用程序)。

當我運行我的應用程序凍結,直至循環完成,即使我運行一個循環的循環。

是否有解決方案,以使其顯示當前文件的名稱?我想這爲好,但它也凍結了我的應用程序:

label2.text = foundfile 

應用程序沒有任何響應點擊,直到它完成了循環。它顯示了Label1中的大小以及Label2中的最後一個掃描文件。這也響應的應用程序:

system.threading.thread.sleep(100) 

有沒有替代的foreach或來解決這個問題的解決方案?

+1

我不熟悉vb.net的語法,但你可能想在新線程而不是UI線程上啓動循環,所以它不會阻塞。 – overloading

+0

我該怎麼做(在一堂課或一個模塊中......) –

+0

我腦海中有兩件事情:1.舊的'BackgroundWorker'。 2.新的「異步」/「等待」關鍵字。 Google針對這些條款。 –

回答

2

下面是使用異步/等待通過單擊按鈕()處理一個簡單的例子:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Button1.Enabled = False 

    Await Task.Run(Sub() 
         ' this runs in a different thread without blocking the GUI: 
         For Each foundFile As String In My.Computer.FileSystem.GetFiles(
            "\windows", Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.*") 
          Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile) 

          ' when you need to update the GUI: 
          Me.Invoke(Sub() 
             ' ... do it in here ... 

             Label1.Text = Label1.Text + filesizelabel.Length 

            End Sub) 
         Next 
        End Sub) 

    Button1.Enabled = True 
End Sub 

2010年VB.Net,試試這個來代替:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Button1.Enabled = False 
    Dim T As New System.Threading.Thread(AddressOf Worker) 
    T.Start() 
End Sub 

Private Sub Worker() 
    ' this runs in a different thread without blocking the GUI: 
    For Each foundFile As String In My.Computer.FileSystem.GetFiles(
        "\windows", Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.*") 
     Dim filesizelabel As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(foundFile) 

     ' when you need to update the GUI: 
     Me.Invoke(Sub() 
         ' ... do it in here ... 

         Label1.Text = Label1.Text + filesizelabel.Length 

        End Sub) 
    Next 

    Me.Invoke(Sub() 
        Button1.Enabled = True 
       End Sub) 
End Sub 
+0

看起來像你的代碼是geat但它不工作。我收到很多**預計的結果**和**期望的聲明** –

+0

您使用的是什麼版本的.Net? –

+0

Vb.Net 2010(.Net 4.0) –

1

這是一個總理候選人爲background worker

有關於它們是如何工作的讀取,但在較高水平的任務與你在你的主UI線程訪問某些事件運行在另一個線程。

Private bw As BackgroundWorker = New BackgroundWorker 

    Private Sub buttonStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
     If Not bw.IsBusy = True Then 
      ' this will start the work 
      bw.RunWorkerAsync() 
     End If 
    End Sub 

    Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
     If bw.WorkerSupportsCancellation = True Then 
      ' this will allow the user to cancel the work part way through 
      bw.CancelAsync() 
     End If 
    End Sub 

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)  
     ' your slow code goes here 
    End Sub 

    Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) 
     ' you can update the UI here to show progress 
    End Sub 

    Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) 
     ' your 'I've finished notification' code goes here 
    End Sub