Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows C#4.0 Application , +
I have a windows form application for displaying sensor values through a GUI. The input for my GUI is a text file that is being continuously updated by a Sensor interrogator. I read the text file, save it onto a list and do my calculations and manipulations on it.
At present my application works only for a static txt file, I want it to be capable of directly reading from the continuously updated txt file.
Can this be done?
I already tried using the FilsSstemWatcher class but to no much use.
 
I tried taking ideas from a codeproject article Use FileSystemWatcher in a Practical Scenario[^]
Posted 4-Feb-13 22:55pm
Edited 4-Feb-13 23:28pm
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Depends. If the software writing the file is written to take an exclusive lock on the file at any time, then something is going to break: either you won't be able to access the file while it is being updated, or the sensor won't be able to update it while you are reading and data could be lost.
 
Writing files often involves exclusive locks (so the data doesn't get corrupted when it is half written by a different application) but exactly how this is done varies. Some applications take an exclusive lock when they start up, and release it when they close (Outlook does this with it's databases annoyingly). Others take the lock, write the data and release the lock (Word does this). If your software works like the latter, then you should be able to read the file, but this may cause problems if the sensor software does not cope well with lock failure - if it doesn't retry, it may well lose data. Basically, you will have to try it and see what happens!
 
[edit]Missing word: "file" - OriginalGriff[/edit]
  Permalink  
v2
Comments
suramrit at 5-Feb-13 5:15am
   
While trying, do I keep my code for reading the txt file i.e should i keep my code same for a static file?
 
What I came up with was to use a filesystemwatcher object on the file... in case of a change event I would copy the change in data in the original file and write it onto a temp file and then do my calculations on it.
Does this seem like a good idea?
PS: i havent tried this with the interrogator but copy-pasted the data in txt file myself(am I doiing something wrong here?)
 
//Here is the code for the Change event for a FileSystemWatcher
void watcher_Changed(object sender, FileSystemEventArgs e)
{


try
{

watcher.EnableRaisingEvents = false;
// string tempPath = @"C:\Users\Schumi\Desktop\test_change.txt";
// string ChangePath = @"C:\Users\Schumi\Desktop\change.txt";



// Read the temp file and store the result in memory
string temp = File.ReadAllText(tempPath);
string difference = string.Empty;
 
// Read newly changed file from Memory
string path = e.FullPath;
StreamReader reader = new StreamReader(path);
string content = reader.ReadToEnd();
reader.Close();
 
// Compare the File in Memory with the temp file and get the difference
if (content.Length > temp.Length)
{
if (temp.Length > 0)
{

difference = content.Replace(temp, "0.001000 0 3 0 3 1544.827243 1553.255697 1566.026953 1545.584506 1554.146420 1566.362399");
}
else
{
difference = content;
}

File.WriteAllText(tempPath, content);
// temp = content.Replace(temp, "");
File.WriteAllText(ChangePath, difference);
}
else
{

File.WriteAllText(tempPath, content);
}
reader_worker.RunWorkerAsync();

}
finally
{
watcher.EnableRaisingEvents = true;
}



//throw new NotImplementedException();
}
OriginalGriff at 5-Feb-13 5:24am
   
Difficult to say - it depends on how the sensor software works. If it holds the file until it closes, then the file system watcher can't see a change until then.
What I would do is read the file content into memory and work on it there, instead of writing it to a temporary file - since you have read it already it makes little sense to work on a file at all.
 
What does the sensor do each time? Append the data, or just provide the current value?
suramrit at 5-Feb-13 5:25am
   
It appends the data, i.e keeps adding rows to it. I still havn't figured out what kind of a lock it has on the file.
OriginalGriff at 5-Feb-13 5:35am
   
If you can read the file while the software is running, then it doesn't take an exclusive lock and hold onto it - so as long as your software can cope with not being able to read it occasionally, you should be fine.
Can you scrap the file? or do you need all the value histories in the original file?
Because what I would do is read the file, and replace it with a new file that contained just the last entry (assuming you need that for some reason)
Then you know that all the entries you have to look at are the new values.
suramrit at 5-Feb-13 5:47am
   
"Can you scrap the file? or do you need all the value histories in the original file?"
does this mean editing the file.. well i havent tried it yet.
OriginalGriff at 5-Feb-13 6:02am
   
What I would try to do is make sure that the file I read contained only new entries - so if you can rewrite the file once you have read it, I'd do that. That way, the file is read for next time - the sensor appends the info each time, so if you code doesn't run for an hour or so, it picks up just the new values since the last time it ran.
If you are only interested in the latest value, it's irrelevant.
suramrit at 5-Feb-13 6:11am
   
Thats some work to do, Ill get to it and see what can be done.. anyways thanks for your input.. :)

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



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100