设置VB.NET事件的优先级

问题描述:

我想创建一个程序,根据输入和与数据库的匹配来创建一堆文件夹。 一切正常,但我想以编程方式仔细检查是否创建了正确数量的文件夹。设置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_CreatedCreateButton_Click执行完毕后处理(所以FOLDERCOUNT仍为0)。

我试图用一个事件来执行检查到该事件的顺序上升和处理将是

  • CreateButton_Click运行

对于i = 1到x的文件夹来创建

  • 1文件夹中创建
  • FSWatcher提高1 “创建” 事件

下一个

  • CreationDone事件引发
  • CreateButton_Click结束
  • X FSWatcher.Created事件的处理
  • CreationDone的处理

但creationdone在获得优先权文件系统事件。

我该如何解决这个问题?

有趣的发现(编辑):当有在文件夹复制过程thown异常,FOLDERCOUNT正常递增,所以异常都让FSWatcher引发事件和/或我的代码处理它们。我是否应故意抛出异常以使其正常工作? (或者只是在我显示msgBox来显示错误时执行)

谢谢。

+0

不幸的是,你不能设置事件优先级,据我知道。您应该更好地记录代码运行时应创建的文件夹的名称,并在完成之后验证它们是否存在。这比使用FileSystemWatcher要好,因为如果您处于可以在其他线程中创建其他文件夹的多线程环境中,则有可能发生这种情况。 –

+0

谢谢,我只是检查目录不存在 - 创建它 - 检查目录存在:) – Ali

不知道你的代码,如果你想只得到文件夹的数量为特定的目录,你可以这样做:

Dim folderCount as Integer = Directory.GetDirectories("YourPathway").Length