-
Sep 16th, 2021, 02:09 PM
#1
Thread Starter
Enjoy the moment
[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
-
Sep 16th, 2021, 02:30 PM
#2
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.
-
Sep 16th, 2021, 02:34 PM
#3
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
thank you amigo
ill check it
-
Sep 16th, 2021, 02:35 PM
#4
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
Originally Posted by dday9
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?
-
Sep 16th, 2021, 02:36 PM
#5
Re: visual studio 2017 listener instead of timer?
I modified the code slightly, but yes, that would go in the Form's load event.
-
Sep 16th, 2021, 02:47 PM
#6
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
something is wrong
Attachment 182337
-
Sep 16th, 2021, 03:16 PM
#7
Re: visual studio 2017 listener instead of timer?
I modified the code slightly (I mentioned this in post #5).
-
Sep 16th, 2021, 03:35 PM
#8
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
there is still an error sir
Attachment 182338
-
Sep 16th, 2021, 03:50 PM
#9
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?
-
Sep 16th, 2021, 04:16 PM
#10
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
no error for created
only this line
Code:
sender As Object, e As FileSystemEventArgs
-
Sep 16th, 2021, 04:42 PM
#11
Thread Starter
Enjoy the moment
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.
-
Sep 16th, 2021, 04:46 PM
#12
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
Originally Posted by dday9
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
-
Sep 16th, 2021, 04:47 PM
#13
Re: visual studio 2017 listener instead of timer?
Is the error still the same?
-
Sep 16th, 2021, 05:05 PM
#14
Thread Starter
Enjoy the moment
Re: visual studio 2017 listener instead of timer?
dday9 thank you
i fixed it
-
Sep 16th, 2021, 06:57 PM
#15
Thread Starter
Enjoy the moment
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|