473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

windows service not working for everyone.

Hi I have already posted this question before.I got a work around but doesn't seem reasonable.

I developed a windows service and I installed it in our server.everybody in the office has access to a folder in the server.anybody can put a file in that shared folder and the service has to pick it up. but it does not pickup for everyone. it just picks up for some users. and the strange thing is it pickes up for rest of the users also but ocassionally.

Iam not getting how to make it work consistantly. any ideas on this issue.

does it have to work under admin account. if so what should i do for that.

please help!
Ayush
Sep 15 '08 #1
30 3567
Hi
also another question right now I have my service running under network service account. what difference will it make if I run it under local system account. I am just accessing local drives on the server no remote or network drives are being accessed so is the local system account enough to do the job?

If anyone can tell me what is the difference between the two accounts it'll be a great help.
Ayush
Sep 15 '08 #2
PRR
750 Expert 512MB
"I developed a windows service and I installed it in our server.everybody in the office has access to a folder in the server.anybody can put a file in that shared folder and the service has to pick it up. but it does not pickup for everyone. it just picks up for some users. and the strange thing is it pickes up for rest of the users also but ocassionally."
"
Iam not getting how to make it work consistantly. any ideas on this issue. "


Can you post a sample code... also are you using FileWatcher? What kinds of error, (in case you are getting) or exceptions are you gettin? Plz post more info and sample code...
Sep 15 '08 #3
Thanks for the quick reply.

yes I am using filewatcher. and the service works fine for me(Since I installed it on the server,I am asuming it'll work for me).

there is no error,service just termiates unexpectedly for some users who try to put a file in that folder. as a recovery action i made the service to restart. when the same user tries to put the file back in the folder it works. i dont know why it stops the first time.

Code:(text)

1. public Service1()
2. {
3. try
4. {
5. _timer = new Timer(6000);
6. InitializeComponent();
7. }
8. catch (Exception ex)
9. {
10. System.IO.StreamWriter tw = new System.IO.StreamWriter("C:\\gggg\\jjjj\\error.txt" , true);
11. tw.Write(ex);
12. tw.Close();
13. }
14. }




15. protected override void OnStart(string[] args)
16. {
17. try
18. {
19. _timer.Start();

20. FileWatch.Path = ConfigurationSettings.AppSettings["WatchPath"];
21. FileWatch.Created += new FileSystemEventHandler(FileWatch_Created);

22. }
23. catch(Exception ex)
{
24. System.IO.StreamWriter tw = new System.IO.StreamWriter("C:\\gggg\\jjjj\\error.txt" , true);
25. tw.Write(ex);
26. tw.Close();
27. }
28. }



29.private void FileWatch_Created(object sender,
30. System.IO.FileSystemEventArgs e)
31. {
32. string[] filedata = File.ReadAllLines(e.FullPath);
33. int maxlins = filedata.Length;

34. for (int i = 0; i < maxlins; i++)
35. {
36. TranslateForm form = new TranslateForm();
37. form.newproperty(filedata, e);

38. }

39. }

hope this provides the information.do let me know if i have to answer any questions
Sep 15 '08 #4
Curtis Rutland
3,256 Expert 2GB
Please start using [code] tags when you are posting your code.

You don't need to put the line numbers there, the forum software will do that for you.

MODERATOR
Sep 15 '08 #5
PRR
750 Expert 512MB
when you are transfering file from network or from computer to a destination folder which has a active watcher... the ownership of file is not available to watcher as soon as file is created... so try and pause for couple of seconds in "watcher_filecreated" ... secondly when using windows service you need to use threading so as to "come back" to onstart as soon as possible...
Sep 15 '08 #6
Do you mean I should use "System.Threading.Timer stateTimer" instead of "System.Timers"
Sep 15 '08 #7
balabaster
797 Expert 512MB
Are you finding that this is inconsistent? i.e. when only one user is using the service it works all the time, but when multiple users use the service, sometimes it fails for some users, but other times it doesn't fail for those users but fails for different users? Like, does it work sometimes for any given user and then fail on other times? It seems to me that this may be a classic deadlocking issue - when the server is trying to write to the file it locks the file and you can't read it during the timespan it takes for the server to write.

If this is what is happening, then you need to do something to alleviate this. I would write a queued read/write mechanism. i.e. you need the server to have an intermediate mechanism that can queue up reads and have clients talk to that mechanism instead of both trying to access the file directly. It works in the same fashion as the buffer on your hard disk. Files aren't accessed directly on the disk, you talk to the buffer, the buffer then gets what is on the disk and returns it to you. Likewise when the file is written, what you are writing goes into the buffer and the buffer writes to the file. This way you can alleviate this deadlocking issue that is caused by file locking.

As an example, the queuing mechanism might have some similar interface:
Expand|Select|Wrap|Line Numbers
  1. Public Class FileInterface
  2.  
  3. Private FileLockMechanism As New Object
  4. Private FilePath As String
  5.  
  6. Private Function WriteContent(ByVal stringToAdd As String) As Boolean
  7.  
  8.   SyncLock FileLockMechanism
  9.     'Do whatever you've gotta do to write to the file
  10.     'Create your writer in here...if you create it outside, it'll lock the file
  11.     'and then nobody will be able to read the file.
  12.   End SyncLock
  13.  
  14. End Function
  15.  
  16. Private Function ReadContent() As String
  17.  
  18.   SyncLock FileLockMechanism
  19.     'Do whatever you've gotta do to read from the file here.  You can create
  20.     'the reader at the class declaration level as the reader doesn't lock the
  21.     'file for writing.  If you're not constantly creating and destroying the reader
  22.     'then you're not wasting cpu cycles unnecessarily.
  23.   End SyncLock
  24.  
  25. End Function
  26.  
  27. Public Sub New(ByVal FileName As String)
  28.   FilePath = FileName
  29. End Sub
You'd then need to set up some kind of protocol for the client services to talk to this service. It's not particularly complex, you can use any number of mechanisms to do this. Remoting is probably the least complex to implement.
Sep 15 '08 #8
Thanks for the reply.
you are right about service being unpredictable there is also another problem with it. service terminates unexpectedly sometimes. when it restarts again it works fine. is this behavior also a deadlocking issue?
Sep 15 '08 #9
balabaster
797 Expert 512MB
Thanks for the reply.
you are right about service being unpredictable there is also another problem with it. service terminates unexpectedly sometimes. when it restarts again it works fine. is this behavior also a deadlocking issue?
I can't be 100% sure... the easiest method I can think to do this is by turning off the software that's writing to file for a moment... make sure all but one of the client services are shut off. Open the file in some simple application that doesn't lock the file - I assume it's a text file, so open it in notepad. Keep writing to the file and saving it and see if you can get the client service to hang or crash out. If it consistently works and doesn't fall over, then that kind of points towards locking causing the problem. Now, turn on all the client services and continue writing and saving with Notepad. They will probably all continue to function just fine. I would hazard a guess that it's the writing mechanism that's causing the issue as opposed to the client services. The client services are likely crashing because they're trying to read the file while it's locked by the server. Consequently if you push all the reads and writes through the same mechanism that streams data into the file and streams data out, then it will alleviate the problem. Alternatively, if you wrap the reads in something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim EndTime As DateTime = DateTime.Now().Add(New Timespan(0,0,10))
  2. While True And Now() < EndTime
  3.   Try
  4.     'Do stuff to read file
  5.     Exit While
  6.    Catch ex As Exception
  7.     'Ignore the error, we don't care
  8.    End Try
  9. End While
This kind of mechanism will continue to attempt to read the file until either it was read successfully or a preset timeout passes - in this case 10 seconds. Of course, the side effect of this is that there may be unnecessary network traffic may be generated during locking events. So as we say in England, it's swings and roundabouts - you make your trade-offs and choose your approach accordingly.

Simple code that's quick to implement catches the exception (which is inherently expensive) and causes potentially large amounts of requests for the file over the network. But it is simple and quick and gets you up and running right now. You just need to be aware of the side effects.

More complex code will prevent having to catch the exception, and will require only a single network call, the server will receive the request to read which will be put into a queue and will respond once the file is unlocked. But on the other hand, now you have to code the protocol for client-server communication, which makes future code maintenance potentially more difficult.
Sep 15 '08 #10
Ok.Got it. I'll work on that and see if it works.

Thanks,
Ayush
Sep 15 '08 #11
on the contrary to what we were discussing, I just realised this service is used probably 3 times in a week randomly. none of us use it consecutively to create any deadlock problem. there is plenty of time for the system to finish reading and writing process even then the service terminates.

strange thing is i restart it after a minute and after restarting it works like a charm.any insight on this?
Sep 15 '08 #12
balabaster
797 Expert 512MB
on the contrary to what we were discussing, I just realised this service is used probably 3 times in a week randomly. none of us use it consecutively to create any deadlock problem. there is plenty of time for the system to finish reading and writing process even then the service terminates.

strange thing is i restart it after a minute and after restarting it works like a charm.any insight on this?
Hmm...can't say for sure. If it's not a locking issue, then it must be that there's some exception that's not being caught and ignored causing the service to crash. What that could be, I couldn't begin to hazard a guess without seeing all the code. What you should do is write an exception handler in the service to catch all exceptions and write them to the event log, or even just a text file so you can see why the service is crashing. Chances are, by doing that, it'll stop crashing, but you should be able to see the errors that were causing it to crash previously, they'll just be handled now.
Sep 15 '08 #13
I tried catching the exception. even the start and stop of service are in try catch blocks. but no exception is recorded since the service just terminates without executing any code.
Sep 15 '08 #14
balabaster
797 Expert 512MB
I tried catching the exception. even the start and stop of service are in try catch blocks. but no exception is recorded since the service just terminates without executing any code.
Are all the methods in all the classes in try/catch blocks? The start and stop are only fired when the service starts or stops, not as you noticed when it crashes. So we need to pinpoint where it is crashing which will require you digging through the methods in each class... and perhaps even the properties.
Sep 15 '08 #15
i guess none of the classes are getting executed. because i added

Code:(text)

static void Main()
{
System.Diagnostics.Debugger.Launch();


and tried debugging the service i dont think system ever reaches to that place for me to debug and see where it is crashing.
Sep 15 '08 #16
Ok,

I debugged it somehow and this is what I am getting

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\W080805.TXT'.


W080805.TXT' is the file that is being placed in the folder and the service has to pick it up. that file is still in the folder and I am getting this error.

Any idea why this is happening.
Ayush
Sep 15 '08 #17
balabaster
797 Expert 512MB
Ok,

I debugged it somehow and this is what I am getting

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\W080805.TXT'.


W080805.TXT' is the file that is being placed in the folder and the service has to pick it up. that file is still in the folder and I am getting this error.

Any idea why this is happening.
Ayush
Is C: on the local machine or the remote machine? Does the file for sure exist?
Sep 15 '08 #18
yes c is in local mahcine i have inserted two files before which are picked up by the service. third one is giving this error. file exists because i have copy pasted it in to that folder for the service to pick it up.
Sep 15 '08 #19
its very wierd but sometimes I am getting this error


An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\W080716.TXT' because it is being used by another process.

W080716.TXT' file is not open anywhere.
Sep 15 '08 #20
balabaster
797 Expert 512MB
its very wierd but sometimes I am getting this error


An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\W080716.TXT' because it is being used by another process.

W080716.TXT' file is not open anywhere.
Does your own application open this file anywhere and not dispose of connections to it? I sometimes see this if I've forgotten a:

sw.close()
sw.dispose()

On the streamwriter that's accessing the file...
Sep 15 '08 #21
no everytime i opened a StreamWriter I have closed it.
Sep 15 '08 #22
balabaster
797 Expert 512MB
no everytime i opened a StreamWriter I have closed it.
Sorry man, then without seeing your code, I've eliminated everything I can think of.
Sep 15 '08 #23
hmm.. i know thanks for trying anyway. I will post my solution if i find any.
Ayush
Sep 15 '08 #24
PRR
750 Expert 512MB
"System.Timers" is better option...
what i had suggested was that you should try to pause... when the watcher event is called....
Also try and optimized the buffer size:
FileSystemWatcher..InternalBufferSize Property .. the default is around 4K which can handle around 400 request per second... (if i am not wrong)..

I would suggest you do this:
1.First Pause on
Expand|Select|Wrap|Line Numbers
  1. private static void OnChanged(object source, FileSystemEventArgs e)
  2.         {
  3.             Thread.Sleep(1000);
  4.             // This is enable the file to be tranfered before your code goes off..
  5.  
  6.         }
  7.  
2. Instead Of writing all processin in OnChanged... Try doin this: inside onchanged ... write to Database the file name and processing status as "False"
3. Periodically run a function using timers to do processin on the file... this way if too many files are put up... then your service wont be creating large number of threads...
4. For testing purpose ... put your code in console program and check... Finally you can transfer to a windows service..
Sep 16 '08 #25
Hi,

I thought the problem may be as you said before: ownership of file is not available to watcher as soon as file is created.so I used Thread.Sleep(1000);I also increased the buffersize of filewatcher. but still the service throws an error that the file that i just copied into the folder is not available. it is not recognizing the file atall.

this service is not used very frequently so there is no problem of processing many files at once.only one file is loaded that too may be 3 times a week.

do you have any idea why does it say

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\W080805.TXT'.
Ayush
Sep 16 '08 #26
balabaster
797 Expert 512MB
I wonder if the network share may be an issue? What is intriguing is that the FileSystemWatcher flags the file as having been created, but that System.IO.FileExists returns false...

Did you try wrapping the copy in:
VB:
Expand|Select|Wrap|Line Numbers
  1. If System.IO.FileExists(e.FullPath) Then
  2. ...
  3. End If
Sep 16 '08 #27
PRR
750 Expert 512MB

I thought the problem may be as you said before: ownership of file is not available to watcher as soon as file is created.so I used Thread.Sleep(1000);I also increased the buffersize of filewatcher. but still the service throws an error that the file that i just copied into the folder is not available. it is not recognizing the file atall.

this service is not used very frequently so there is no problem of processing many files at once.only one file is loaded that too may be 3 times a week.

do you have any idea why does it say

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\W080805.TXT'.
Ayush
wow... now i m run out of ideas ... try testing it from another pc.... one more thing post your code where exception occurs and also the full exception message and stacktrace
i will go through ur post again .. n find something .. that will be 2moro.. meanwhile give as much info as possible ... so that we can trace the error...

as u have mentioned u r not using "C:\W080805.TXT"...still wats d content of it...
Sep 16 '08 #28
after a lot of work i found a work around not a solution though.i used Thread.Sleep(6000); and also i placed FileWatch_Created code in if loop saying if any file exists then read it else go to FileWatch_Created again.

because as i increase the time in thread.sleep the service was getting delayed. so now till the service finds a file it will remain in the if else loop. it wont be a infinite loop since it is going to find a file eventually. there is a creation of file that is why it has entered into FileWatch_Created event.

i dont think this is the best method but it worked.
Thanks to all who helped.

Ayush
Sep 16 '08 #29
balabaster
797 Expert 512MB
after a lot of work i found a work around not a solution though.i used Thread.Sleep(6000); and also i placed FileWatch_Created code in if loop saying if any file exists then read it else go to FileWatch_Created again.

because as i increase the time in thread.sleep the service was getting delayed. so now till the service finds a file it will remain in the if else loop. it wont be a infinite loop since it is going to find a file eventually. there is a creation of file that is why it has entered into FileWatch_Created event.

i dont think this is the best method but it worked.
Thanks to all who helped.

Ayush
Sorry you couldn't find a more ideal solution - it does seem like a bit of a hack to do it that way. But at least you have something until you find a way to resolve the issue properly...
Sep 16 '08 #30
PRR
750 Expert 512MB
after a lot of work i found a work around not a solution though.i used Thread.Sleep(6000); and also i placed FileWatch_Created code in if loop saying if any file exists then read it else go to FileWatch_Created again.

because as i increase the time in thread.sleep the service was getting delayed. so now till the service finds a file it will remain in the if else loop. it wont be a infinite loop since it is going to find a file eventually. there is a creation of file that is why it has entered into FileWatch_Created event.

i dont think this is the best method but it worked.
Thanks to all who helped.

Ayush
good work finally you cracked it....
Sep 17 '08 #31

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andrei Prodan | last post by:
Hi everyone, I have a small piece of code which uses GetObject function and it is working fine in a windows application developed under .NET In a windows service application, the GetObject...
4
by: Simon Niederberger | last post by:
Hi I need to create a MessageQueue in my C# service (running as SYSTEM). Users will have no permissions on this queue, so I can't look if messages are present. When setting...
4
by: Tim Golden | last post by:
Tim Golden enlightened us with: > > Well, I'm with you. I'm sure a lot of people will chime in to point > > out just how flexible and useful and productive Linux is as a > > workstation, but every...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
7
by: Simon Harvey | last post by:
Hi everyone, I need to make a service that monitors a directory for changes in the files contained within it. I have two questions: 1. I'm going to be using a FileSystemWatcher object to do...
0
by: pithhelmet | last post by:
Hello everyone - I need to create a windows service that will watch for a specific dialog, and when found, send a keypress to the dialog. I have the service working, but when it tries to find...
0
by: artsohc | last post by:
Hey Everyone, this is my first time posting so go easy on me. I am trying to hook up music-on-hold at the office I work at. I got all the music loaded and I got Windows Media Player working while...
1
by: icecroft | last post by:
Hello everyone! We're running a windows service application and it gets this error: "the service did not respond to the start or control request in a timely fashion". It works in most machines...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.