Solved

Windows Service Problem with FileSystemWatcher and SQLCommand

Posted on 2004-09-16
7
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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!
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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