I am reading data from serial port and writing it on text field using Timer event , but when writing those data to file in timer event method or SerialDataReceived method. I get file is being used by another thread.
Even I set FileShare.Write , but still having the same problem.
From your problem description, I think you are trying to write the file from two places Timer event and SerialDataReceived event. So there is a chance of both trying to access the file at the same time. Better and a synchronizing block using lock. It could be better to understand the problem if the source code can be shared.
I changed the algorithm ,usage a Array List for storing the data coming from the
SerialDataRecievedEvent then usage timer event to write into the file !
It worked !
Related
I am using serialport in forms. My pc is linked with a external device through serialport.
I try to multi-write a sequence of data into the device with feedback read.
When I write the data one by one through mouse click, everything is good. But when I try to write-wait-read-write-wait-read.... in one thread. The feedback read comes back in a cluster of data at the end of the write instead of after every write.
my code looks like this
void button click write and read
{
serialport.write
wait
read
write
wait
read
...
}
I tried to invoke read function in the function and multi-thread read. But it doesn't seem to work. I guess the thread processes the main loop before processes all the reads functions.
My idea to fix this is to tail write function to the read function.
Anyone knows how the thread prioritizes function? Or a better idea to fix this?
Thanks
How I may know which file is modified and what data is changed in the file?
Edit: I want to watch the file as it gets modified and then compare it against a previous version to know which data blocks are changed. I guess watching the file for changes can be accomplished by using file watcher API but I have no idea about the second part.
You may need the FileSystemWatcher class.
The most common approach is define FileSystemWatcher, subscribe to its events and process them accordingly to the logic of your application.
Here is a simple example.
I have a method which save settings to file. This method is called if value of dateTimePicker changed. But I have in Form_Load loading settings -> I read value from file and assign it with dateTimePicker, but this call method save_settings (couse value changed). And in this moment is problem couse the file is open by program -> reading values and program wanna write to file changes...
How can I do that?
I think that you have critical section. There are plenty ways to deal with this issue . One way is to put Lock statement around file saving . This way one thread should wait after another thread is finished. But from understanding your question I think that problem is with your desing . As I understand you trying to read and write simultaneously . Maybe you should declare global bool variable isToSave . That will indicate when you can save . When working with file do not forget to use using statement to release file
Handle.
Use some kind of flag - set it when you start reading your config and unset it in finally block. When the flag is set, ignore calls to ValueChanged. Since you are loading config in OnLoad, there will be no other reasons for dateTimePicker's value change because you're in main UI thread and message pump is not pumping at the moment.
I have a program that reads an XML file (for now, on local computer.) and loads the data into a list of struct.
How can I make it such that if I execute it, it does the above but then waits to keep checking for any change to the file. Should the file be changed, it reads the file all over again.
Do I need to create a file watcher service as described here:
http://www.codeproject.com/KB/files/C__FileWatcher.aspx
You need FileSystemWatcher - the docs give examples.
Basically you create an instance, give it a filter (which would be your exact file in this case), hook up an event handler (probably the Changed event in your case) and then set EnableRaisingEvents to true.
You'll want to look at the System.IO.FileSystemWatcher class. You can have it raise an event in your code when the file is changed.
Details can be found on MSDN: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
Look at the FileSystemWatcher class. You can point it at your XML file and when it changes, it will fire an event so you can then read the file again
Is there a built in method for waiting for a file to be created in c#? How about waiting for a file to be completely written?
I've baked my own by repeatedly attempting File.OpenRead() on a file until it succeeds (and failing on a timeout), but spinning on a file doesn't seem like the right thing to do. I'm guessing there's a baked-in method in .NET to do this, but I can't find it.
What about using the FileSystemWatcher component ?
This class 'watches' a given directory or file, and can raise events when something (you can define what) has happened.
When creating a file with File.Create you can just call the Close Function.
Like this:
File.Create(savePath).Close();
FileSystemWatcher can notify you when a file is created, deleted, updated, attributes changed etc. It will solve your first issue of waitign for it to be created.
As for waiting for it to be written, when a file is created, you can spin off and start tracking it's size and wait for it stop being updated, then add in a settle time period, You can also try and get an exclusive lock but be careful of locking the file if the other process is also trying to lock it...you could cause unexpected thigns to occur.
FileSysWatcher cannot monitor network paths. In such instances, you manually have to "crawl" the files in a directory -- which can result in the above users error.
Is there an alternative, so that we can be sure we don't open a file before it has been fully written to disk and closed?