Program terminates without exception details
-
martes, 17 de noviembre de 2009 20:08
Hi,
After dealing with the pain-in-the-____ error "The path is not on a legal form" witch only occured after dragging a file system watcher onto the form, by creating the file watcher in the code. When an event is raised, i have to tell the user what happened. I can do this using messageboxes, without any problem. But, when I wanted the output to be displayed in a textbox, the program terminates without any warning.
Here is what my code looks like:
Public Class Form1 Public watchfolder As FileSystemWatcher Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load watchfolder = New System.IO.FileSystemWatcher() watchfolder.Path = "c:\temp" watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _ IO.NotifyFilters.FileName watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _ IO.NotifyFilters.Attributes watchfolder.Filter = "*.*" AddHandler watchfolder.Changed, AddressOf logchange AddHandler watchfolder.Created, AddressOf logchange AddHandler watchfolder.Deleted, AddressOf logchange AddHandler watchfolder.Renamed, AddressOf logrename watchfolder.EnableRaisingEvents = True TextBox1.Text = "Changelogging started." End Sub Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Changed Then MsgBox("Item Updated: " & e.FullPath) End If If e.ChangeType = IO.WatcherChangeTypes.Created Then TextBox1.Text &= "Item Created" End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then MsgBox("Item Deleted: " & e.FullPath) End If 'code above works perfectly well, unlike the code below End Sub Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) TextBox1.Text &= "File" & e.OldName & _ " has been renamed to " & e.Name & vbCrLf e.Name) End Sub End ClassI have no clue on how to fix this, also because i don't get an error or any output from VS
Thanks in advance, Brecht
Todas las respuestas
-
lunes, 29 de agosto de 2011 9:37
Hi BDrecht,
As a general solution to your problem, i suggest you wrap the code contents inside try catch block. From your code i inferred that you didn't used Try ,catch blocks at all as result it is hard for the compiler to catch exception if there is any.Regards,
A.Murugan -
lunes, 29 de agosto de 2011 23:25
Hi Brecht,
You can't update a control in another thread other than the thread of creating it.
Please refer http://msdn.microsoft.com/en-us/library/ms171728.aspx#Y1096 for details.
I definitely agree what Tech Murugan said, it is a good idea to have a try catch for debug use.
BTW, I updated your code of Rename and it works now FYI:
Delegate Sub SetTextCallback([text] As String) Private Sub SetText(ByVal [text] As String) ' InvokeRequired required compares the thread ID of the ' calling thread to the thread ID of the creating thread. ' If these threads are different, it returns true. If Me.TextBox1.InvokeRequired Then Dim d As New SetTextCallback(AddressOf SetText) Me.Invoke(d, New Object() {[text]}) Else Me.TextBox1.Text &= [text] End If End Sub Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) SetText("File" & e.OldName & _ " has been renamed to " & e.Name) End Sub

