Solved

Windows Service Problem with FileSystemWatcher and SQLCommand

Posted on 2004-09-16
7
271 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Accepted Solution

by:
Agarici earned 125 total points
Comment Utility
[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_
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now