|
What are you doing with this information that you would need to save it every second then read it from another watcher program when it changes??
Cleako
|
|
|
|
|
Hi,
'the other software' is the frontend of a measurement system writing measurement data continuously (well, nearly - once per second). It's a closed system with no other access to the measurement data, postprocessing is supposed to be done via Excel. I started writing a 'real time postprocessing' for this application - that's why and what for I have to use this workaround.
Best regards, bodobe
|
|
|
|
|
Does the log file need to exist indefinitely or just until the post processor works with it?
Cleako
|
|
|
|
|
Once I have read the data lines into my software, I can write a new logfile. So the original logfile is no longer required.
BR, bodobe
|
|
|
|
|
You could do that then. Create a new file for each entry. In the post processor always start from the oldest file, read it in, then delete it after closing the file. That way you are never attempting to read a file you are writing to or the other way around.
Cleako
|
|
|
|
|
I think you can use the a FileStream object to randomly access a file. Here is some code for you to look over. I added a button to the form and when you click the button a messagbox appears displaying the last text to be added to a text file. It keeps track of the last position read so the next time it reads the file it can begin reading it from the last position read. To test it just create a file "c:\test.txt". Run the application and then modify the text file and save it. Clicking the button should display the text you entered. Open the file again and add another line and save it. Again clicking the button should show you the new line of text.
Public Class Form1
Dim index As Long = -1
Const file As String = "c:\test.txt"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'FileInfo object so we can get the file size
Dim info As New IO.FileInfo(file)
'How many new bytes since we last checked
Dim count As Long = info.Length - index
'File hasn't changed so return
If count <= 0 Then Return
'File stream to read file
Dim stream As IO.FileStream = IO.File.Open(file, IO.FileMode.Open, IO.FileAccess.Read)
'Buffer to retrieve bytes
Dim buf(count) As Byte
'Set the streams position to where we last left off
stream.Position = index
'Read the new bytes
stream.Read(buf, 0, count)
'clean-up so we don't get in the way of the other program
stream.Close()
stream.Dispose()
'Decoder to convert byte array to string
Dim textDecoder As New System.Text.ASCIIEncoding
'The new text
Dim str As String = textDecoder.GetString(buf)
'Display new text
MsgBox(str)
'Set index to next position
index = info.Length
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialize index to the last position in the file
Dim info As New IO.FileInfo(file)
index = info.Length
End Sub
End Class
|
|
|
|
|
Hi, thanks a lot, works great! I only had to place the IO.File.Open(...) into a Try..Catch, because the FileSystemWatche raises the FileChanged-Event a bit too early: File has been changed, but not yet been closed by the other software.
While LogReadPermission <> True
Try
stream = IO.File.Open(LogFile, FileMode.Open, FileAccess.Read)
LogReadPermission = True 'if stream has been opened without exception, allow reading and leave 'while'
Catch ex As Exception
Threading.Thread.Sleep(5) 'short wait before next try
i += 1
If i > 100 Then
MsgBox("Logfile not found! " & ex.ToString)
End If
End Try
End While
stream.Position = Index
stream.Read(buf, 0, count)
...
|
|
|
|
|
Your welcome, glad it's working for you. I messed around with the code after I posted it. I also added a try catch block around the IO.File.Open and looped until it was a success with a small wait in between each attempt. Looks like with think a like. Just so you know retrieving the FileInfo object will succeed even if the file doesn't exist. However, if you try to read the file size and the file doesn't exist it will throw an error which would crash your program if you didn't handle it. The info object can actually tell you if the file exists. I check this and leave the procedure if we have no file. This is how I handled it.
Do
'Get the most up to date information
info = New IO.FileInfo(file)
'If file doesn't exist leave
If Not info.Exists Then Return
'info.length throws an error if the file doesn't exist, thus the needed line above.
count = info.Length - index
Try
stream = IO.File.Open(file, IO.FileMode.Open, IO.FileAccess.Read)
Catch ex As Exception
'Sleep a little and try again later
Threading.Thread.Sleep(5)
End Try
Loop While stream Is Nothing
I like your counter to keep track of attempts and respond if there are too many. That's odd that the file isn't ready for reading after the FileSystemWatcher raises the event. I simulated your situation and did a stress test for 3 minutes and it never failed to open the file. Maybe it has to do with the file size. I started from scratch so the file was pretty small.
Just out of curiosity does this logfile ever get cleared? A line a second has to add up.
|
|
|
|
|
One suggestion, if you program by exception your app will slow down tremendously. If you can find a way to avoid having it throw the exception in order for you to know when to try to access again then you should.
CleAkO
|
|
|
|
|
Hello.
I've already made my own form to use a GUI to search for a specific folder to save information to, but I was wondering if anyone knew a way to use the CommonDialog to do this?
Currently, I can only get the CommonDialog to save to a specific file. What I want is for my user to be able to search and pick a specific folder. I just want to use the Common Dialog box to return the path to that folder.
Is there any way to do this or should I just stick with my homemade method?
Thank you!
|
|
|
|
|
Hello all. I've been sent a code of a program in VB6. However, there was no *.vbp file. I opened the *.frm and *.bas files and it seems to be encrypted. In fact, when I open one of these files with VB6, it throws me an error saying that it is encrypted, and it won't open it. Any idea why is this happening and what can I do to open those files?
Regards.
A polar bear is a bear whose coordinates has been changed in terms of sine and cosine.
Personal Site
|
|
|
|
|
VB6 never, nor even gave the option to, encrypted the source files of a project. The files you're talking about are always saved in plain text. It looks like what you got was incomplete and corrupted. Get the files from your source again and see what they send.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Yep, I know. I was thinking that there might be some kind of plug in or something, since the source actually is encrypted.
Thanks.
A polar bear is a bear whose coordinates has been changed in terms of sine and cosine.
Personal Site
|
|
|
|
|
hii all i need some one to give me any refrencce or books to learn ho can i use or control for serial port i hope the book with arabic language plz
|
|
|
|
|
i use a component MSCOMM for serial ports.
in this i need to pass all settings of device.
and its generates a txt file, i use that file to get the specifc data
i think should try this kind of thing
bye
Thanks,
Ankur Bakliwal
|
|
|
|
|
I am trying top disable a groupbox through code. Can someone please help? Here is some code to see what I am trying to do. I want to disable the groupbox where you see the code GroupBox1.Enabled = False. This does not work, any ideas?
Dim message As DialogResult
'Confirm deletion of rows from table in database
message = MessageBox.Show("Do you want to delete old data?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If message = Windows.Forms.DialogResult.Yes Then
DeleteQuery()
ElseIf message = Windows.Forms.DialogResult.No Then
MessageBox.Show("Since old data is not deleted you may encounter scanning issues. Are you sure you want to keep old data?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
If message = Windows.Forms.DialogResult.Yes Then
GroupBox1.Enabled = False
ElseIf message = Windows.Forms.DialogResult.No Then
DeleteQuery()
End If
End If
jds1207
|
|
|
|
|
You never set your message value again for the 2nd messagebox.
Cleako
|
|
|
|
|
GroupBox1.Enabled = False will disable a groupbox. That code works just fine. The problem is it never runs. The groupbox get's disabled if the user clicked yes. However that if statement is located within an another if statement that only executes if the user pressed no. The user can't press no and yes. When in doubt add stops to your code so you can tell if code is being executed.
|
|
|
|
|
Hi I wrote a VB code for automatic date picker,I need to convert the same to Javascript inorder to embbed the same into Infopath.Can you please me in this.Below is the VB code.
Private Sub Cmd_Timer_Click()
Dim a As String
Dim b As Date
b = DTPicker1
'a = day(DTPicker1)
'MsgBox a
If Combo1.Text = Label4.Caption Then
Text1.Text = DTPicker1 + 1
DTPicker1 = Text1.Text
a = Format$(DTPicker1, "dddd")
If a = "Sunday" Then
DTPicker1 = DTPicker1 + 1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
Else
If a = "Saturday" Then
DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
End If
End If
DTPicker1 = b
Exit Sub
End If
If Combo1.Text = Label5.Caption Then
Text1.Text = DTPicker1 + 2
DTPicker1 = Text1.Text
a = Format$(DTPicker1, "dddd")
If a = "Sunday" Then
DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
Else
If a = "Saturday" Then
DTPicker1 = DTPicker1 + 3
'Text1.Text = DTPicker1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
End If
'Text1.Text = DTPicker1 + Text2.Text
End If
DTPicker1 = b
Exit Sub
End If
If Combo1.Text = Label6.Caption Then
For i = 1 To Text2.Text
a = Format$(DTPicker1 + i, "dddd")
If a = "Sunday" Then
DTPicker1 = DTPicker1 + 1
'Text1.Text = DTPicker1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
Else
If a = "Saturday" Then
DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
End If
End If
Next i
'Text1.Text = Format(DTPicker1, "MM/DD/YYYY") + Text2.Text
Text1.Text = Format(DTPicker1 + Text2.Text, "MM/DD/YYYY")
DTPicker1 = b
Exit Sub
End If
If Combo1.Text = Label7.Caption Then
Text1.Text = Format(DTPicker1, "MM/DD/YYYY")
End If
'DTPicker1 = Text1.Text
'a = Format$(DTPicker1, "dddd")
'If a = "Sunday" Then
'DTPicker1 = DTPicker1 + 1
'Text1.Text = DTPicker1
'Else
'If a = "Saturday" Then
'DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
'End If
'End If
End Sub
Private Sub Combo1_click()
'Label4.Caption = Combo1.Text
If Combo1.Text = "3(User Defined)" Then
Text2.Visible = True
Else
Text2.Visible = False
End If
'Dim a As String
'Dim b As Date
'b = DTPicker1
'
''a = day(DTPicker1)
''MsgBox a
'
'If Combo1.Text = 1 Then
'Text1.Text = DTPicker1 + 1
'DTPicker1 = Text1.Text
'a = Format$(DTPicker1, "dddd")
'If a = "Sunday" Then
'DTPicker1 = DTPicker1 + 1
'Text1.Text = DTPicker1
'Else
'If a = "Saturday" Then
'DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
'End If
'End If
'End If
'If Combo1.Text = 2 Then
'Text1.Text = DTPicker1 + 2
'DTPicker1 = Text1.Text
'a = Format$(DTPicker1, "dddd")
'If a = "Sunday" Then
'DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
'Else
'If a = "Saturday" Then
'DTPicker1 = DTPicker1 + 3
'Text1.Text = DTPicker1
'End If
''Text1.Text = DTPicker1 + Text2.Text
'End If
'End If
'If Combo1.Text = 3 Then
'For i = 1 To Text2.Text
'a = Format$(DTPicker1 + i, "dddd")
'If a = "Sunday" Then
'DTPicker1 = DTPicker1 + 1
'Text1.Text = DTPicker1
'Else
'If a = "Saturday" Then
'DTPicker1 = DTPicker1 + 2
'Text1.Text = DTPicker1
'End If
'End If
'
'Next i
'Text1.Text = DTPicker1 + Text2.Text
'End If
'
''DTPicker1 = Text1.Text
''a = Format$(DTPicker1, "dddd")
''If a = "Sunday" Then
''DTPicker1 = DTPicker1 + 1
''Text1.Text = DTPicker1
''Else
''If a = "Saturday" Then
''DTPicker1 = DTPicker1 + 2
''Text1.Text = DTPicker1
''End If
''End If
'
'
'DTPicker1 = b
End Sub
Private Sub Form_Load()
DTPicker1.Value = Now()
End Sub
|
|
|
|
|
There are plenty of javascript calendar controls. No-one is going to rewrite this for you.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hello,
Does anybody know how to use ado recordset to add new record and save it in VB.NET? I have unboung textboxes and I need be able to add new record and save it to Caller records table?
thanks,
programmer
|
|
|
|
|
Do you mean saving from VB6 to VB.NET?
Cleako
|
|
|
|
|
Hi,
No. I have VB.NET (visual studio 2005) form and I would like to add, and save new records after clicking on a button? Table is called caller records and located on sql server express edition.
thanks,
programmer
|
|
|
|
|
Why are you using ADO ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
I have three drop down lists in page1. DDL2 is values are dependant on DDL1, DDL3 values are dependant on DDL2. Once I select DDL values I have submit button that takes the values from these DDL values and redirects to appropriate page based on case statements. Now the issue is the new loaded page is opening with DDL values default to their first value rather than retaining the vales from prevous page values. Can someone help on retaining the values of the DDL between response.redirects.
Appreciate all the help
Raj D
|
|
|
|