Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows Service and Threading

Windows Service and Threading

Scheduled Pinned Locked Moved C#
tutorialhelp
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Saamir

    Can somebody please give me an example of a windows service that uses threading to look at a folder for files and if it finds a file then does some processing. Upon completion of processing looks again and continues... I don't know if it is wise to keep looking at a folder every second for files, please advice on that. If it is not then maybe put a wait and look every 20 minutes or so. Please help!! I don't need code to look for files, etc. I just need some guidance on how to build the service with a thread. Thank you in advance

    Sameer

    A Offline
    A Offline
    Abhijit Jana
    wrote on last edited by
    #4

    Ok, there are multiple way to do that. As Luc suggested you can use FileSystemWatcher Class to check a folder. If any new files created it will fire an event and based on that you can do the required operation. you can use TimerDelegate with windows service to monitor on a specific duration. Recentenly I have completed a similar application. Please let me know if you have more doubts. Hope I can help you in this. Thank you.

    cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

    S 1 Reply Last reply
    0
    • A Abhijit Jana

      Ok, there are multiple way to do that. As Luc suggested you can use FileSystemWatcher Class to check a folder. If any new files created it will fire an event and based on that you can do the required operation. you can use TimerDelegate with windows service to monitor on a specific duration. Recentenly I have completed a similar application. Please let me know if you have more doubts. Hope I can help you in this. Thank you.

      cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

      S Offline
      S Offline
      Saamir
      wrote on last edited by
      #5

      hi Abhijit, Thank you for your response. I went with the following procedure and the service seems to be running fine. Please advice if this is not a good idea for a windows service http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread[^]

      Sameer

      A 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi again, there is a FileSystemWatcher class that monitors changes to a folder or file; the problem with it (and with any other method of observation you could possibly come up with) is that it recognizes changes, rather than states, i.e. it will tell you a file got created, got written to, etc, but it is unable to signal a file has reached a new and consistent state (there is no FileClosed event or something similar). The only really good way is for the file creator to signal you when a file create or update has been executed to completion. I see no need to use threading, unless maybe if you want to watch several folders. You might want to tell us more about what the files are about, and what you intend to do to them. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        S Offline
        S Offline
        Saamir
        wrote on last edited by
        #6

        Hi Luc, As always thank you for your response. My windows service in short looks at 3 folders for .csv files, parses them and does some database updating. From time to time there are files added to these folders so I have to keep looking at a certain interval in these folders (currently configured every 20 minutes) I decided to implement the following method: http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread[^] Please let me know if this is not a good method, I would hate to crash the server because that is where this service will be loaded :) Thanks

        Sameer

        M L 2 Replies Last reply
        0
        • S Saamir

          hi Abhijit, Thank you for your response. I went with the following procedure and the service seems to be running fine. Please advice if this is not a good idea for a windows service http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread[^]

          Sameer

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #7

          Samir, That is fine. I have one question regarding your application. How file are submitting into the Folder? Is it submitted by any client? Is there any possibilites the more than thousand files created on that folder?

          cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

          S 1 Reply Last reply
          0
          • A Abhijit Jana

            Samir, That is fine. I have one question regarding your application. How file are submitting into the Folder? Is it submitted by any client? Is there any possibilites the more than thousand files created on that folder?

            cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

            S Offline
            S Offline
            Saamir
            wrote on last edited by
            #8

            Happy to hear that it is a good method. A different client creates and dumps these files. Usually, there is a single file at one point and what my application is doing is processing these files and dumping it to an Archive folder.

            Sameer

            A 1 Reply Last reply
            0
            • S Saamir

              Happy to hear that it is a good method. A different client creates and dumps these files. Usually, there is a single file at one point and what my application is doing is processing these files and dumping it to an Archive folder.

              Sameer

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #9

              I was asking because in my case there can be possiblity of many file submitted at the same time, because files are submitted via Web services. So I was wondaring with behaviour of FileSystemWatcher that it can manage that hughe change list or not. So what I did, I have stored the file name in DB while client storing the file into the folder, means my Web service save the posted file and store the Filename in DB . Now what I did, Windows Service read the DB load 100 Files name in DataSet, Process them one by one and update the dataset, After complete the all processing I have update DB with the updated dataset and Load the next 100 Records. Now If there is no records, I have used a DelegateTimer which will monitor the database with 15 min interval to file come. If file is there, I stopped the current timer and process those data.

              cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

              S 1 Reply Last reply
              0
              • A Abhijit Jana

                I was asking because in my case there can be possiblity of many file submitted at the same time, because files are submitted via Web services. So I was wondaring with behaviour of FileSystemWatcher that it can manage that hughe change list or not. So what I did, I have stored the file name in DB while client storing the file into the folder, means my Web service save the posted file and store the Filename in DB . Now what I did, Windows Service read the DB load 100 Files name in DataSet, Process them one by one and update the dataset, After complete the all processing I have update DB with the updated dataset and Load the next 100 Records. Now If there is no records, I have used a DelegateTimer which will monitor the database with 15 min interval to file come. If file is there, I stopped the current timer and process those data.

                cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

                S Offline
                S Offline
                Saamir
                wrote on last edited by
                #10

                what I am doing is reading the file names into a string array and then using foreach loop to process each file. This is incase there are multiple files. It seems to be doing a good job with the threading and the thread.sleep. I wonder if there are more system resources being used with my approach, I will monitor the error log and hopefully there won't be any issues :)

                Sameer

                A 1 Reply Last reply
                0
                • S Saamir

                  what I am doing is reading the file names into a string array and then using foreach loop to process each file. This is incase there are multiple files. It seems to be doing a good job with the threading and the thread.sleep. I wonder if there are more system resources being used with my approach, I will monitor the error log and hopefully there won't be any issues :)

                  Sameer

                  A Offline
                  A Offline
                  Abhijit Jana
                  wrote on last edited by
                  #11

                  Saamir wrote:

                  thread.sleep

                  Hmmm... Its time for Abhijit.Sleep(). Its alreay 2.50 AM here :), still some pending work in the ToDo List... . And Again office Tomorrow .. Sorry Today :( :sigh: :doh: .

                  cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net

                  1 Reply Last reply
                  0
                  • S Saamir

                    Hi Luc, As always thank you for your response. My windows service in short looks at 3 folders for .csv files, parses them and does some database updating. From time to time there are files added to these folders so I have to keep looking at a certain interval in these folders (currently configured every 20 minutes) I decided to implement the following method: http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread[^] Please let me know if this is not a good method, I would hate to crash the server because that is where this service will be loaded :) Thanks

                    Sameer

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #12

                    We do exactly the same thing except we use a winforms app to give feedback to the user, it chacks every 2 minutes and disables the timer while checking (otherwise eyou get multiple threads) We have implemented a delay in the reading of the file to avoid touching files still being written by setting a flag with the time of first seeing the file, if more than # period has elapsed since seeing the file we can then process it. This has been working perfectly for the past 4 years, at 2am it processes up to 1200 files an hour.

                    Never underestimate the power of human stupidity RAH

                    1 Reply Last reply
                    0
                    • S Saamir

                      Hi Luc, As always thank you for your response. My windows service in short looks at 3 folders for .csv files, parses them and does some database updating. From time to time there are files added to these folders so I have to keep looking at a certain interval in these folders (currently configured every 20 minutes) I decided to implement the following method: http://en.csharp-online.net/Creating_a_.NET_Windows_Service%E2%80%94Alternative_1:_Use_a_Separate_Thread[^] Please let me know if this is not a good method, I would hate to crash the server because that is where this service will be loaded :) Thanks

                      Sameer

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      Hi, one way to overcome the problem you don't get events when a file creation/update is completed is this, assuming you're in charge of the producer's code: - look for "event files", recognized by a specific characteristic, maybe a new file extension .EVE - create/update your data files in any way you see fit; when done, create a short .EVE file containing the full path of the data file. Such file will be written atomically since it fits in a single sector. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups