Results 1 to 15 of 15

Thread: [RESOLVED] visual studio 2017 listener instead of timer?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Resolved [RESOLVED] visual studio 2017 listener instead of timer?

    hey
    i have a timer that each 10 seconds checks a file that is comming from an api.
    question is there other method for this?
    i know there is a way called listener
    dont know how to do it.
    this is the code im using
    Code:
       Private Sub TmrCheckFile_Tick(sender As Object, e As EventArgs) Handles TmrCheckEmvFile.Tick
    
            Try
    
                Dim FileName As String = "Charge.out"
                Dim FilePath As String = "C:\ESystem\EOUT" & "\" & FileName
                If File.Exists(FilePath) Then
                    TmrCheckEmvFile.Enabled = False
                    Call BtnOk.PerformClick()
                End If
    
            Catch ex As Exception
                TmrCheckFile.Enabled = False
                BtnClose.Enabled = True
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    any help will be much appriciated
    regards
    salsa 31

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,505

    Re: visual studio 2017 listener instead of timer?

    Take a look at the FileSystemWatcher class (documentation). Here is a quick (untested) example:
    Code:
    Using watcher = New FileSystemWatcher("C:\path\to\folder")
        With watcher
            .NotifyFilter = NotifyFilters.CreationTime
            .Filter = "*.out"
            .EnableRaisingEvents = True
    
            AddHandler .Created, Sub(sender As Object, e As FileSystemEventArgs)
                                               Dim path As String = IO.Path.Combine("C:", "ESystem", "EOUT", "Charge.out")
                                               If (File.Exists(path)) Then
                                                   BtnOk.PerformClick()
                                               End If
                                           End Sub   
        End With
    End Using
    Last edited by dday9; Sep 16th, 2021 at 02:35 PM.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    thank you amigo
    ill check it

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    Quote Originally Posted by dday9 View Post
    Take a look at the FileSystemWatcher class (documentation). Here is a quick (untested) example:
    Code:
    Using watcher = New FileSystemWatcher("C:\path\to\folder") With {
        .NotifyFilter = NotifyFilters.CreationTime
        .Filter = "*.out"
        .EnableRaisingEvents = True
    
        AddHandler .Created, AddressOf Sub(sender As Object, e As FileSystemEventArgs)
                                           Dim path As String = IO.Path.Combine("C:", "ESystem", "EOUT", "Charge.out"0
                                           If (File.Exists(path)) Then
                                               BtnOk.PerformClick()
                                           End If
                                       End Sub   
    }
    End Using
    where do i put this code?
    in form load?

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,505

    Re: visual studio 2017 listener instead of timer?

    I modified the code slightly, but yes, that would go in the Form's load event.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    something is wrong
    Attachment 182337

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,505

    Re: visual studio 2017 listener instead of timer?

    I modified the code slightly (I mentioned this in post #5).
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    there is still an error sir
    Attachment 182338

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,505

    Re: visual studio 2017 listener instead of timer?

    sender and e will need to be renamed to avoid the conflicting parameter names of load. What is the error for created?
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    no error for created
    only this line
    Code:
    sender As Object, e As FileSystemEventArgs

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    Lambda parameter 'sender' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression.

  12. #12

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    Quote Originally Posted by dday9 View Post
    sender and e will need to be renamed to avoid the conflicting parameter names of load. What is the error for created?
    changed it
    still not working
    this is the whole code
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      
            Using watcher As New FileSystemWatcher("C:\ESystem\EOUT")
                With watcher
                    .NotifyFilter = NotifyFilters.CreationTime
                    .Filter = "*.out"
                    .EnableRaisingEvents = True
                    AddHandler .Created, Sub(asender As Object, ae As FileSystemEventArgs)
                                             Dim path As String = IO.Path.Combine("C:\", "ESystem", "EOUT", "Charge.out")
                                             If File.Exists(path) Then
                                                 MessageBox.Show("file found", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                             End If
                                         End Sub
                End With
            End Using
    
    
        End Sub

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,505

    Re: visual studio 2017 listener instead of timer?

    Is the error still the same?
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: visual studio 2017 listener instead of timer?

    dday9 thank you
    i fixed it

  15. #15

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,193

    Re: [RESOLVED] visual studio 2017 listener instead of timer?

    btw
    if the file is found
    how do i disable the handler for not calling again and again?
    and when i want to read it i get access denied
    Last edited by salsa31; Sep 16th, 2021 at 07:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width