Link to home
Start Free TrialLog in
Avatar of EM77
EM77

asked on

Windows Service Problem with FileSystemWatcher and SQLCommand

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!
 

Avatar of Razzie_
Razzie_

Sounds like you want to use the lock statement. It's hard to explain without some code to apply it to, but using the lock statement prevents two threads from accessing one object at the same time. Reading this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfLockStatement.asp should make things pretty clear. If you still need specific info, let me know.
i think you can solve the problem by adding "[MethodImpl(MethodImplOptions.Synchronized)]" to the function being called simultaneus by two different threads.

hth.
A.
Avatar of EM77

ASKER

I think I need to explain with code my problem;
I have this two events calling the same function:

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
  {
   //Make a local copy of the specific file
   CopiarDescargaVINCLE(e);

   //Fill the SqlTable TRANSFERS
   OmplirBBDDTRANSFERS("TRANCABE",e);
   }

private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e)
  {
   //Make a local copy of the specific file
   CopiarDescargaVINCLE(e);
   
   //Fill the SqlTable TRANSFERS
   OmplirBBDDTRANSFERS("TRANDETA",e);
  }

Where I have to use the lock statement or where I have to add "[MethodImpl(MethodImplOptions.Synchronized)]" ???

The two functions CopiarDescargaVINCLE or OmplirBBDDTRANSFERS needs to be lock by only one thread and the other event need to wait untill the first event will be finished...

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Agarici
Agarici
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OR:

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
 {
    lock(this)
    {
      //Make a local copy of the specific file
      CopiarDescargaVINCLE(e);

      //Fill the SqlTable TRANSFERS
      OmplirBBDDTRANSFERS("TRANCABE",e);
    }
}

private void fileSystemWatcher2_Created(object sender, System.IO.FileSystemEventArgs e)
 {
   lock(this)
   {
     //Make a local copy of the specific file
     CopiarDescargaVINCLE(e);
 
     //Fill the SqlTable TRANSFERS
    OmplirBBDDTRANSFERS("TRANDETA",e);
   }
}
Avatar of EM77

ASKER

Ok, maybe the two methods works propiertly, but now I'm using the [MethodImpl(MethodImplOptions.Synchronized)] solution.

What do you think that's the better way to prevent my problem???