Windows Service Problem with FileSystemWatcher and SQLCommand
Posted on 2004-09-16
I have a window service that get two kind of files created in a server specific folder, read those files and insert those data in a SqlServer Database.
To get those file information I'm using two FileSystemWatcher (FileSystemWatcher1 and FileSystemWatcher2) Components and to insert the data that thosefiles contain I'm using only one function.
This function read line by line of the specific file and insert data to a Sql Table usind SQL Insert Command.
If those files are created in diferent time there is no problem. The service runs perfectly and insert the data in the SQL.
The problem begins when those files are created exactly in the same time so the two FileSystemWatcher_Created events begins at the same time and the to files objects call the same function. Then the lines of code of this function are mixed and I have problems like when one file open a connection and the other file reopen this connection the service throw an exeption, or when one file close the connection the other file still using inserts commands, etc...
What is the better way for prevent that problem???
Thanks in advance!