Continuous file monitoring problem
-
I am basically an electronics student. I am currently developing a vb code to control multiple devices using bluetooth. I am making use of vb here. I am a very beginner in vb and dont have much knowledge. Please help me with examples. Problem: Below, I have provided the code which I developed for my project. It monitors few text files located in C:\ directory and based on that, the relevant code is sent through parallel port which I use to further purposes. The problem occurred that I need to monitor those files. I used loop, do, if, goto, while statements. Till the end button is pressed, it needs to monitor the existence of files. All the statements resulted in "NOT RESPONDING" state. Can anyone please help me for that. I just want my program to loop continuously from end to beginning till the end button(command2) is pressed. Then I used a check button and used if command, it also did not work. Here is the code I wrote till now -----------------------------------
Private Sub Command1_Click() ' following sections to read file line obtained from http://www.codeguru.com/forum/archive/index.php/t-315872.html Dim strFile As String Dim PortAddress As Integer 'Dim iFile As Integer Dim strContent As String Dim strContent2 As String Dim strContent3 As String Dim strContent4 As String PortAddress = &H378 'TO Do Add code here to put a filename with path in strFile Label1.Caption = "light is off" 'display status Label3.Caption = "fan is off" 'display status file1 = "c:\lighton.txt" file2 = "c:\lightoff.txt" file3 = "c:\fanon.txt" file4 = "c:\fanoff.txt" 'iFile = FreeFile '1=lighton.txt 2=lightoff.txt 3=fanon.txt 4=fanoff.txt 'restart: Do If (Len(Dir$(file1)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lighton.txt" For Input As #1 'The following will read the first line from the file Line Input #1, strContent ' read 1st line in the file Close #1 ' close file If (strContent = "light on") Then 'check validity of file Label1.Caption = "light is on" 'display status Value = Inp(PortAddress) Value = Value Or &HF 'make lsbs high Out PortAddress, Value 'output the value End If For i = 1 To 5 If (Len(Dir$(file2)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lightoff.txt" For Input
-
I am basically an electronics student. I am currently developing a vb code to control multiple devices using bluetooth. I am making use of vb here. I am a very beginner in vb and dont have much knowledge. Please help me with examples. Problem: Below, I have provided the code which I developed for my project. It monitors few text files located in C:\ directory and based on that, the relevant code is sent through parallel port which I use to further purposes. The problem occurred that I need to monitor those files. I used loop, do, if, goto, while statements. Till the end button is pressed, it needs to monitor the existence of files. All the statements resulted in "NOT RESPONDING" state. Can anyone please help me for that. I just want my program to loop continuously from end to beginning till the end button(command2) is pressed. Then I used a check button and used if command, it also did not work. Here is the code I wrote till now -----------------------------------
Private Sub Command1_Click() ' following sections to read file line obtained from http://www.codeguru.com/forum/archive/index.php/t-315872.html Dim strFile As String Dim PortAddress As Integer 'Dim iFile As Integer Dim strContent As String Dim strContent2 As String Dim strContent3 As String Dim strContent4 As String PortAddress = &H378 'TO Do Add code here to put a filename with path in strFile Label1.Caption = "light is off" 'display status Label3.Caption = "fan is off" 'display status file1 = "c:\lighton.txt" file2 = "c:\lightoff.txt" file3 = "c:\fanon.txt" file4 = "c:\fanoff.txt" 'iFile = FreeFile '1=lighton.txt 2=lightoff.txt 3=fanon.txt 4=fanoff.txt 'restart: Do If (Len(Dir$(file1)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lighton.txt" For Input As #1 'The following will read the first line from the file Line Input #1, strContent ' read 1st line in the file Close #1 ' close file If (strContent = "light on") Then 'check validity of file Label1.Caption = "light is on" 'display status Value = Inp(PortAddress) Value = Value Or &HF 'make lsbs high Out PortAddress, Value 'output the value End If For i = 1 To 5 If (Len(Dir$(file2)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lightoff.txt" For Input
Take a look at the FileWatcher class (I think) this works for small file numbers (chokes when there are too many events. Also you should change your loop to a timer based application, something like Start time Timer tick event Turn timer off Process directory turn timer on Set the tick for 1 second
-
I am basically an electronics student. I am currently developing a vb code to control multiple devices using bluetooth. I am making use of vb here. I am a very beginner in vb and dont have much knowledge. Please help me with examples. Problem: Below, I have provided the code which I developed for my project. It monitors few text files located in C:\ directory and based on that, the relevant code is sent through parallel port which I use to further purposes. The problem occurred that I need to monitor those files. I used loop, do, if, goto, while statements. Till the end button is pressed, it needs to monitor the existence of files. All the statements resulted in "NOT RESPONDING" state. Can anyone please help me for that. I just want my program to loop continuously from end to beginning till the end button(command2) is pressed. Then I used a check button and used if command, it also did not work. Here is the code I wrote till now -----------------------------------
Private Sub Command1_Click() ' following sections to read file line obtained from http://www.codeguru.com/forum/archive/index.php/t-315872.html Dim strFile As String Dim PortAddress As Integer 'Dim iFile As Integer Dim strContent As String Dim strContent2 As String Dim strContent3 As String Dim strContent4 As String PortAddress = &H378 'TO Do Add code here to put a filename with path in strFile Label1.Caption = "light is off" 'display status Label3.Caption = "fan is off" 'display status file1 = "c:\lighton.txt" file2 = "c:\lightoff.txt" file3 = "c:\fanon.txt" file4 = "c:\fanoff.txt" 'iFile = FreeFile '1=lighton.txt 2=lightoff.txt 3=fanon.txt 4=fanoff.txt 'restart: Do If (Len(Dir$(file1)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lighton.txt" For Input As #1 'The following will read the first line from the file Line Input #1, strContent ' read 1st line in the file Close #1 ' close file If (strContent = "light on") Then 'check validity of file Label1.Caption = "light is on" 'display status Value = Inp(PortAddress) Value = Value Or &HF 'make lsbs high Out PortAddress, Value 'output the value End If For i = 1 To 5 If (Len(Dir$(file2)) <> 0) Then 'lighton.txt file existance check For j = 1 To 255 Close #j Next j Open "c:\lightoff.txt" For Input
Wow, this is some old code. Warning - This code will NOT work with VB.NET and won't work at all under a Windows NT Kernel O/S, such as WIndows 2000, XP, 2003, Vista, ... What version of Basic is this and what O/S are you planning on running it under?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007