Click here to Skip to main content
15,887,404 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
when my file system watcher detects a virus a dialog shows but when i click the delete file option it says its open in my program but in the filesystem watcher when i add the openfiledialog.Dispose function it doesnt show my dialog so heres the code can somone provide a fix? code below.


Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Try
            Detect.Labellastreal.Text = e.FullPath
            ListBox3.Items.Add(Detect.Labellastreal.Text)
            Me.OpenFileDialog3.FileName = ""
            Dim scanbox As New TextBox
            scanbox.Text = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\VirusList.dat").ToString
            Dim md5 As New MD5CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If scanbox.Text.Contains(buff.ToString) Then
                Me.OpenFileDialog3.FileName = e.FullPath
                Detect.ShowDialog()


                WriteToLog("Virus detected")

            End If

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim ex As Exception = exception1
            ProjectData.ClearProjectError()
        End Try
    End Sub


What I have tried:

i said in the first one that i tried OpenFileDialog.Dispose method but didnt work :(
can someone please provide a fix thanks in advance.
Posted
Updated 5-Jun-18 0:21am

1 solution

You probably need to use Invoke, as it is not permitted to change the UI controls from another thread, see: Control.Invoke Method (Delegate, Object[]) (System.Windows.Forms)[^]
And the answer here: invoke in other class[^]
Here is an example, for a function without parameters, and also for a function with parameters: Accessing Controls from Worker Threads-VBForums[^]
Here is a page from the book .NET Gotcha's about Invoke: NET Gotchas: 75 Ways to Improve Your C# and VB.NET Programs - Venkat Subramaniam - Google Boeken[^]
 
Share this answer
 
v4
Comments
Helpmecodeplz 19-Aug-17 9:23am    
thanks dude but i dont understand them links how do i add the invoke into the code i wrote above ? thanks for answering so quickly though, but could you explain what code i write and where cheers dude.
RickZeeland 19-Aug-17 9:34am    
I'm more of a C# user, but I think you need something like this:
If Me.InvokeRequired Then
' your OpenFileDialog code ...
Me.Invoke(...)
I will try to find a good example for you, but so far I have found nothing.
Helpmecodeplz 19-Aug-17 9:47am    
thank you so when you say your open file dialog code do you mean OpenFileDialog.Dispose or the code from showing the dialog ect
but really thanks for helping me out dude
RickZeeland 19-Aug-17 9:58am    
No, not only Dispose but any use of a user interface control, which can be a TextBox or Button too !
Helpmecodeplz 19-Aug-17 10:18am    
thanks for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900