Solved

Windows Service Problem with FileSystemWatcher and SQLCommand

Posted on 2004-09-16
7
274 Views
Last Modified: 2011-08-18
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!
 

0
Comment
Question by:EM77
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Razzie_
ID: 12077617
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.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 12082070
i think you can solve the problem by adding "[MethodImpl(MethodImplOptions.Synchronized)]" to the function being called simultaneus by two different threads.

hth.
A.
0
 

Author Comment

by:EM77
ID: 12082138
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!
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Accepted Solution

by:
Agarici earned 125 total points
ID: 12082156
[MethodImpl(MethodImplOptions.Synchronized)]
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);
   }

[MethodImpl(MethodImplOptions.Synchronized)]
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);
  }


hth.
A.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12082404
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);
   }
}
0
 

Author Comment

by:EM77
ID: 12082747
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???
0
 
LVL 11

Expert Comment

by:Agarici
ID: 12084449
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question