MultipleFileSystemWatcher während Event keine lokalen Variablen zur Verfügung?

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Deathmean.

    MultipleFileSystemWatcher während Event keine lokalen Variablen zur Verfügung?

    Hi,
    ich hab mir mit Hilfe von [VB 2008] MultipleFileSystemWatcher eine Klasse MultipleFileSystemWatcher gebastelt:

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.ComponentModel
    3. Public Class MultipleFileSystemWatcher
    4. Public Event Created As EventHandler(Of FileSystemEventArgs)
    5. Private lWatcher As List(Of FileSystemWatcher)
    6. Public Sub New()
    7. lWatcher = New List(Of FileSystemWatcher)
    8. End Sub
    9. Public Sub New(ByVal Paths() As String)
    10. Me.New()
    11. For Each Path As String In Paths
    12. Me.AddPath(Path)
    13. Next
    14. End Sub
    15. Public Sub New(ByVal Paths() As String, ByVal Pattern As String)
    16. Me.New()
    17. For Each Path As String In Paths
    18. Me.AddPath(Path, Pattern)
    19. Next
    20. End Sub
    21. Public Sub AddPath(ByVal Path As String)
    22. Me.AddPath(Path, "*.*")
    23. End Sub
    24. Public Sub AddPath(ByVal Path As String, ByVal Pattern As String)
    25. lWatcher.Add(NewWatcher(Path, Pattern))
    26. End Sub
    27. Public Sub RemovePath(ByVal Path As String)
    28. For Each fsw As FileSystemWatcher In lWatcher
    29. If fsw.Path = Path Then
    30. lWatcher.Remove(fsw)
    31. End If
    32. Next
    33. End Sub
    34. Public Sub RemoveAll()
    35. lWatcher.Clear()
    36. End Sub
    37. Private Function NewWatcher(ByVal Path As String, ByVal Pattern As String) As FileSystemWatcher
    38. Dim fsw As New FileSystemWatcher(Path, Pattern)
    39. AddHandler fsw.Created, AddressOf fswCreated
    40. fsw.EnableRaisingEvents = True
    41. Return fsw
    42. End Function
    43. Private Sub fswCreated(ByVal sender As Object, ByVal e As FileSystemEventArgs)
    44. OnEvent(sender, e, CreatedEvent)
    45. End Sub
    46. Private Sub OnEvent(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs, ByVal [event] As System.Delegate)
    47. Form1.mfsw_ausgelöst(e.FullPath)
    48. End Sub
    49. End Class


    In meinem Hauptcode sieht es dann so aus:

    VB.NET-Quellcode

    1. Private WithEvents mfsw As New MultipleFileSystemWatcher
    2. Dim alle_von As New List(Of String)
    3. Dim alle_nach As New List(Of String)
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5. alle_von.Add("test")
    6. alle_nach.Add("test2")
    7. End Sub
    8. Public Sub mfsw_ausgelöst(ByVal pfad As String)
    9. MsgBox(alle_von(0))
    10. End Sub


    Aber solange ich in dieser Sub bin sind alle lokalen Variablen leer. Wenn ich da nach das z.B. mit einem Button teste steht wieder was drinne.

    Wo ist der Fehler?

    Deathmean schrieb:

    VB.NET-Quellcode

    1. Private Sub OnEvent(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs, ByVal [event] As System.Delegate) Form1.mfsw_ausgelöst(e.FullPath) End Sub
    Das geht vor die Hose.
    Du benötigst eine Instanz von Form1:

    VB.NET-Quellcode

    1. Dim frm As New Form1
    2. ' oder
    3. CALL_TO_CLASS(Me)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    So wie ich mich an meinen Code erinnern kann steht im OnEvent Sub was anderes drin als bei dir.

    Wenn du etwas an der Klasse änderst und es dann nicht mehr funktioniert darfst du dich nicht Wundern. Die Klasse ist so wie sie ist voll funktionsfähig und man sollte nur drim rumfummeln wenn man auch weiß was man tut.

    Mit Events hast du wohl auch noch nicht gearbeitet. Also stelle die Klasse mal wieder so her wie im Original und dann mache dein FormCode

    VB.NET-Quellcode

    1. Private WithEvents mfsw As New MultipleFileSystemWatcher
    2. Dim alle_von As New List(Of String)
    3. Dim alle_nach As New List(Of String)
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5. alle_von.Add("test")
    6. alle_nach.Add("test2")
    7. End Sub
    8. Public Sub mfsw_ausgelöst(ByVal pfad As String) mfsw.Created
    9. MsgBox(alle_von(0))
    10. End Sub