Solved

Windows Service Problem with FileSystemWatcher and SQLCommand

Posted on 2004-09-16
7
276 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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