long processing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Khayralla
    New Member
    • Oct 2008
    • 10

    long processing

    Hi all

    I am developing a small windows Service application which mointer a files in a specific folder and add their names to a queue by using a watch file class to add files to the queue.
    This part is done.
    Then in a windows Service I should move these files to another location (folder,DVD,... ).

    How can I do this task ?

    I am using now


    Code:
    protected override void OnStart(string[] args)
    {
    
        do
    
        {
            if (queue.Count > 0)
    
            {
                itemFile nextFile = (itemFile ) queue.Dequeue();
                System.Threading.Thread t = new
                System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ProcessRequest));
                t.Start(nextFile as object );
            }
        }
    } while (true);
    ..
    and I feel it is not the optimal way.
    Can any one suggest best than the above code

    thanks

    Khayralla
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Have you tried using the File.Move Method to move the files?

      Comment

      • Khayralla
        New Member
        • Oct 2008
        • 10

        #4
        Originally posted by insertAlias
        Please enclose your posted code in [code] tags (See How to Ask a Question).

        This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

        Please use [code] tags in future.

        MODERATOR

        thanks, it was the first post for me

        Comment

        • Khayralla
          New Member
          • Oct 2008
          • 10

          #5
          Originally posted by Frinavale
          Have you tried using the File.Move Method to move the files?
          My question is about
          Code:
          do
          {
          ...
          }while (true);
          maybe it will not execute others events which must be done by this service.
          I mean I have to add Sleep(5000) or no need for it.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            It sounds like you want to implement a Windows Service. Did you create a Windows Service project when you first created your project?

            You need to initialize your file watcher in your OnStart method of your Windows Service application.

            You will also have to implement a method that handles the FileWatcher.Cha nged event.

            The Changed event is fired whenever there is a change in the folder that it's watching....it is at this time that you want to move the file (or do whatever you need to do).


            You don't have to loop forever...just handle the event.

            <edit> Please note that you can not use F5 to run Windows Service applications... .see How To Start Services </edit>

            -Frinny
            Last edited by Frinavale; Oct 30 '08, 04:19 PM. Reason: added more information

            Comment

            • Khayralla
              New Member
              • Oct 2008
              • 10

              #7
              Originally posted by Frinavale
              It sounds like you want to implement a Windows Service. Did you create a Windows Service project when you first created your project?

              You need to initialize your file watcher in your OnStart method of your Windows Service application.

              You will also have to implement a method that handles the FileWatcher.Cha nged event.

              The Changed event is fired whenever there is a change in the folder that it's watching....it is at this time that you want to move the file (or do whatever you need to do).


              You don't have to loop forever...just handle the event.

              -Frinny
              All what you mentioned are done.
              I need to handle the Queue after it enqueued with some files?
              say there are 10 files in the queue.

              So it is better to use ininfint while loop or I have to use another thread ?
              I don't want to block CPU from doing other activities in my infinit while loop.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by Khayralla
                All what you mentioned are done.
                I need to handle the Queue after it enqueued with some files?
                say there are 10 files in the queue.

                So it is better to use ininfint while loop or I have to use another thread ?
                I don't want to block CPU from doing other activities in my infinit while loop.
                Did you implement the Changed event?
                Could you please post your code for this...I don't understand where you are creating a queue or why.

                I'm not very strong with Threading but I know it's not a good idea to have an infinite loop creating threads..this is going to gobble up your resources.

                Comment

                • Curtis Rutland
                  Recognized Expert Specialist
                  • Apr 2008
                  • 3264

                  #9
                  I think your method is pretty good. Probably better than processing them all on the same thread.

                  Comment

                  • Khayralla
                    New Member
                    • Oct 2008
                    • 10

                    #10
                    Originally posted by Frinavale
                    Did you implement the Changed event?
                    Could you please post your code for this...I don't understand where you are creating a queue or why.

                    I'm not very strong with Threading but I know it's not a good idea to have an infinite loop creating threads..this is going to gobble up your resources.
                    Code:
                     protected override void OnStart(string[] args)
                            {
                    
                                pq = new Queue();
                                FileSystemWatcher watcher = new FileSystemWatcher();
                                watcher.Path = myPath;
                                watcher.NotifyFilter = NotifyFilters.FileName;
                                watcher.Filter = "*.txt";
                                watcher.Renamed += new RenamedEventHandler(OnRenamed);
                                watcher.EnableRaisingEvents = true;
                                do
                                {
                                    if (pq.Count > 0)
                                    {
                    	           string nextFile = (string) queue.Dequeue();
                                          ProcessRequest(nextFile);
                                    }
                                } while (true);
                            }
                            private static void OnRenamed(object source, RenamedEventArgs e)
                            {
                                string x = "some thing";
                                pq.Enqueue(x);
                            }
                    Now, Queue is fill with files name and I want to manage the files inside it?

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Originally posted by insertAlias
                      I think your method is pretty good. Probably better than processing them all on the same thread.
                      Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

                      The way I understand it:

                      When the Service starts it checks if there is stuff that has to be processed...

                      The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

                      So how does the looping benefit the OP in his solution?

                      Comment

                      • Khayralla
                        New Member
                        • Oct 2008
                        • 10

                        #12
                        Originally posted by Frinavale
                        Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

                        The way I understand it:

                        When the Service starts it checks if there is stuff that has to be processed...

                        The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

                        So how does the looping benefit the OP in his solution?
                        Frinavale
                        1) what do you mean by OP ?
                        2) FileWatcher will add the file name to the queue, and there is another events will add some files to the queue, so the FileWatcher do what is supposed to do, adding to the queue.

                        later, I have to check the queue, pop first item then burn it on a DVD,
                        pop the 2nd and save it in another folder, etc...
                        So I need to watch the Queue this time, if it contain entries, process the request.
                        so it need to be in infinit loop.

                        any way, I was asking may be the ininfint loop is not a good solution, I am looking for creating an event that will fired if there are some files in the queue, or if the Queue.count > 0, which is better than ininfint loop.

                        thanks

                        Comment

                        • Curtis Rutland
                          Recognized Expert Specialist
                          • Apr 2008
                          • 3264

                          #13
                          Originally posted by Frinavale
                          Maybe I'm not thinking straight but why does the OP need an infinite loop to do this?

                          The way I understand it:

                          When the Service starts it checks if there is stuff that has to be processed...

                          The FileWatcher class should watch the folder and do any processing only when the Changed event is fired....it could create a new thread at that point to do the file processing if it's getting fired a lot...

                          So how does the looping benefit the OP in his solution?
                          I was just answering the question of whether it's better to use processes or handle them all in the current thread. I don't know enough about the OP's question to answer the rest.

                          Khayralla, OP means "Original Poster," which of course, would refer to you. So Frinny was asking me why you needed a loop.

                          Comment

                          • Curtis Rutland
                            Recognized Expert Specialist
                            • Apr 2008
                            • 3264

                            #14
                            Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Originally posted by insertAlias
                              Does anyone know of an object like a Queue, that has events? Something like an event that fires when an item is enqueued/dequeued?
                              Maybe create a new object that inherits from the Queue....

                              Comment

                              Working...