Question

Hey all i am using the FileSystemWatcher to check to see if an .ini file has been changed. If it has then i would like for me to be able to update a textbox on my form. Problem being is that its a shared function in order for the FileSystemWatcher to work so anything on the form cant be access inside that shared function it seems?

My code:

Private Sub frmCamera_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim watcher As New FileSystemWatcher()

    watcher.Path = Application.StartupPath
    watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
    watcher.Filter = "*.ini"
    watcher.SynchronizingObject = Me

    AddHandler watcher.Changed, AddressOf OnChanged
    watcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
    Dim sb1 As New StringBuilder(500)
    Dim theButtonsName As String = ""

    GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")

    main.GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")

    Dim frm As frmCamera = DirectCast(DirectCast(source, FileSystemWatcher).SynchronizingObject, frmCamera)
    frm.TextBox1.Text = "debug: " & sb1.ToString
End Sub

I am calling the form above by the main form like so:

Public Sub doStuff(ByVal what2Do As String)
   If Trim(lanSent(0)) = "camera" And Trim(lanSent(1)) = "show" Then
      theCam = New Threading.Thread(AddressOf ShowCam)

      theCam.SetApartmentState(Threading.ApartmentState.STA)
      theCam.Start()
   Else
     .....
   End If
End Sub

Private Shared Sub ShowCam()
    Dim ShowCam As New frmCamera
    ShowCam.ShowDialog()
End Sub

What i am trying to accomplish is to run some other stuff once it sees that textbox has input in it (TextBox1_TextChanged).

Any help would be great to solve this problem of mine! Thanks!

Was it helpful?

Solution

Not sure if this will work for you since I do not know why you are using a Shared EventHandler, but you can try using theFileSystemWatcher.SynchronizingObject Property if you set this to your Form you can retrieve it in your shared method.

Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
    Dim sb1 As New StringBuilder(500)
    Dim theButtonsName As String = ""
    GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")

    Dim frm As Form1 = DirectCast(DirectCast(source, FileSystemWatcher).SynchronizingObject, Form1)
    frm.TextBox1.Text = sb1.ToString
 End Sub

Intialization of the FileSystemWatcher

 Dim watcher As New FileSystemWatcher
 watcher.Path = Application.StartupPath
 watcher.SynchronizingObject = Me  'Assigning your Forms Instance to the SychronizingObject
 watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
 watcher.Filter = "*.ini"
 AddHandler watcher.Changed, AddressOf OnChanged
 watcher.EnableRaisingEvents = True
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top