Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 4-Feb-13 23:28pm
v4

1 solution

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]
 
Share this answer
 
v2
Comments
suramrit 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 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 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 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 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900