我想創建一個程序,根據輸入和與數據庫的匹配來創建一堆文件夾。 一切正常,但我想以編程方式仔細檢查是否創建了正確數量的文件夾。設置VB.NET事件的優先級
Private Sub CreateButton_Click(sender As Object, e As EventArgs) Handles CreateButton.Click
Dim rows As DataRow()
rows = dTable.Select(String.Format("[Pick List ID] = '{0}'", PLTextBox.Text))
Dim sourcePath As String = ""
Dim destPath As String = ""
If rows.Count > 0 And PLTextBox.Text <> "" Then
' Some variable i want to use to count the number of folders created.
folderCount = 0
For Each row As DataRow In rows
Try
...
some logic to specify what the sourcePath and destPath will be
...
' Checking if the directory exists
If Not (Directory.Exists(destPath)) Then
Directory.CreateDirectory(destPath)
End If
' Copy pasting source directory into newly created folder
FileIO.FileSystem.CopyDirectory(sourcePath, destPath)
Catch ex As Exception
MsgBox("Error: " & ex.Message,, "Something went wrong...")
End Try
Next
所以在這裏,一旦我的所有文件夾的創建,我想驗證正確的數字被創造了,所以在同一子,我有:
RaiseEvent CreationDone(sender, e)
End Sub
要算我的文件夾中,我使用:
Private Sub FSWatcher_Created(sender As Object, e As FileSystemEventArgs) Handles FSWatcher.Created
folderCount = folderCount + 1
End Sub
而且CreationDone事件是:
Private Sub Creation_Done() Handles Me.CreationDone
Dim rows As DataRow() = dTable.Select(String.Format("[Pick List ID] = '{0}'", PLTextBox.Text))
If folderCount <> rows.Count Then
MsgBox(folderCount & " folders were created. " & rows.Count & " were supposed to be created.",, "Error during the folder creation")
Else
MsgBox(rows.Count & " jobs were created",, "Success!")
End If
End Sub
並執行以下操作: 我CreateButton_Click
處理程序運行時,它提出了在年底CreationDone
事件,但沒有更新的文件夾數,因爲我FileSystemWatcher
事件是由FSWatcher_Created
CreateButton_Click
執行完畢後處理(所以FOLDERCOUNT仍爲0)。
我試圖用一個事件來執行檢查到該事件的順序上升和處理將是
CreateButton_Click
運行
對於i = 1到x的文件夾來創建
- 1文件夾中創建
FSWatcher
提高1 「創建」 事件
下一個
CreationDone
事件引發CreateButton_Click
結束- X
FSWatcher.Created
事件的處理 CreationDone
的處理
但creationdone在獲得優先權文件系統事件。
我該如何解決這個問題?
有趣的發現(編輯):當有在文件夾複製過程thown異常,FOLDERCOUNT正常遞增,所以異常都讓FSWatcher
引發事件和/或我的代碼處理它們。我是否應故意拋出異常以使其正常工作? (或者只是在我顯示msgBox
來顯示錯誤時執行)
謝謝。
不幸的是,你不能設置事件優先級,據我知道。您應該更好地記錄代碼運行時應創建的文件夾的名稱,並在完成之後驗證它們是否存在。這比使用FileSystemWatcher要好,因爲如果您處於可以在其他線程中創建其他文件夾的多線程環境中,則有可能發生這種情況。 –
謝謝,我只是檢查目錄不存在 - 創建它 - 檢查目錄存在:) – Ali