FileSystemObject / Thread / Application Hangs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I use FileSystemWatcher in a module with a Main() function that loads some
Forms (Window Forms Application). The starting object of the application is
not a hidden form but a module. I have to use the Application.Run()
statement, without that the application exits. FileSystemWatcher watches the
creation of a xml file called IN.xml. when a file is created,
FileSystemWatcher fires the Create Event but when I try to show the Form, the
application hangs.

Someone has an answer or a documentation which can explain that "problem".

A dummy example :
-----------------------
Public Module MainMod

Private WithEvents FSW As IO.FileSystemWatcher
Private applicContexte As System.Windows.Forms.ApplicationContext
Private Frm As Form

Public Sub Main()
FSW = New IO.FileSystemWatcher("c:\", "IN.xml")
FSW.NotifyFilter = IO.NotifyFilters.LastWrite Or
IO.NotifyFilters.FileName Or IO.NotifyFilters.DirectoryName Or
IO.NotifyFilters.LastAccess
FSW.EnableRaisingEvents = True
applicContexte = New System.Windows.Forms.ApplicationContext
Application.Run(applicContexte)
End Sub

Private Sub FSW_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles FSW.Created
Frm = New Form
Frm.Show()
Frm.Controls.Add(New TextBox)
Frm.Refresh()
End Sub

End Module

Thank for your help !
 
Frederic H said:
Hi,

I use FileSystemWatcher in a module with a Main() function that loads some
Forms (Window Forms Application). The starting object of the application is
not a hidden form but a module. I have to use the Application.Run()
statement, without that the application exits. FileSystemWatcher watches the
creation of a xml file called IN.xml. when a file is created,
FileSystemWatcher fires the Create Event but when I try to show the Form, the
application hangs.
....

Hi,
I suspect FileSystemWatcher is raising event on separate thread. You can
access UI only from main thread.

Goran
 
I found a solution...

<----------
' Declare a delegate to call the function asynchronously
Private Delegate Sub LaunchEventDelegate()
------------>


<----------
' In the fired event of the FilesyStemWatcher (Created)
Dim objDelegate As LaunchEventDelegate
objDelegate = AddressOf FCTToLaunch
objDelegate.BeginInvoke(Nothing, Nothing)
------------>


<----------
' The called function which raise an event
Private Sub FCTToLaunch()
RaiseEvent Event123(...,...,...,...)
End Sub
------------>


<----------
' Context of this object can handle the event and open forms, etc.
Private Sub Foo(...,...,...,...) handles obj.Event123
Frm = New Form
Frm.Show()
Frm.Controls.Add(New TextBox)
Frm.Refresh()
Application.Run(Frm)
End Sub
------------>
 
Back
Top