long processing

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

    #16
    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?
    insertAlias, it is a good question.
    thanks

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #17
      I'll look into it, see if I can't figure something like that out. That would solve your problem pretty well, right?

      Comment

      • Khayralla
        New Member
        • Oct 2008
        • 10

        #18
        Originally posted by insertAlias
        I'll look into it, see if I can't figure something like that out. That would solve your problem pretty well, right?
        yes, so there is no need for infinte loop.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #19
          This is the code for a class called QueueV2 that extends Queue to provide events for when items are Enqueued/Dequeued.

          QueueV2
          Code:
          using System;
          using System.Collections.Generic;
          
          namespace QueueV2
          {
              public delegate void QueueV2EventHandler(object sender, EventArgs e);
          
              public class QueueV2<T> : Queue<T>
              {
                  #region Custom Events
          
                  public event QueueV2EventHandler ItemEnqueued;
                  public event QueueV2EventHandler ItemDequeued;
          
                  protected virtual void OnItemEnqueued(EventArgs e)
                  {
                      ItemEnqueued(this, e);
                  }
                  protected virtual void OnItemDequeued(EventArgs e)
                  {
                      ItemDequeued(this, e);
                  }
          
                  #endregion
          
                  #region Original Methods
          
                  public new void Enqueue(T item)
                  {
                      base.Enqueue(item);
                      OnItemEnqueued(new EventArgs());
                  }
                  public new T Dequeue()
                  {
                      T value = base.Dequeue();
                      OnItemDequeued(new EventArgs());
                      return value;
                  } 
          
                  #endregion
              }
          }
          Here's how you would create a new queue and assign handlers to the events.
          Code:
          QueueV2<string> qv2 = new QueueV2<string>();
          qv2.ItemEnqueued += new QueueV2EventHandler(qv2_ItemEnqueued);
          qv2.ItemDequeued += new QueueV2EventHandler(qv2_ItemDequeued);
          Here's how you would define your handlers.
          Code:
          void qv2_ItemDequeued(object sender, EventArgs e)
          {
            //whatever
          }
          
          void qv2_ItemEnqueued(object sender, EventArgs e)
          {
            //whatever 
          }
          I enjoyed the opportunity to learn how to do this.

          Hope it helps.

          Comment

          • Khayralla
            New Member
            • Oct 2008
            • 10

            #20
            I will test it, I hope it will fit with my needs.
            thanks a lot.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #21
              Originally posted by Khayralla
              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
              Its nice to think that you can just take action as soon as the FileWatcher tells you there is a new file... but you can't.

              SystemFileWatch er fires a New event as soon as the file begins to be written to hard drive, not when it is done. So if you try to act on it too soon it may not be finished being written. If you look for a Changed event, I have generally seen three for most files. I *think* they are related to the created and modified dates being written/changed upon completion as well as the size of the file changing while it is being written. You see all of this clearly if you are writing across a network.

              My solution in the past is to add the file path to a simple string[] then run through the array once every couple seconds from a timer. If a file completes whatever processing it is meant to have (move, print, whatever) then it can come off the list. This allows it to remain in its original location, or not, as needed by your program.

              Comment

              • Khayralla
                New Member
                • Oct 2008
                • 10

                #22
                thanks tlhintoq

                Actually I am copying files with a different extension (say .part), and when it is fininhed copying, the extension will be change to another ext. So the FilewatchSyatem will watch all files *.part, and then it will be fired when there is a change in the ext.
                thanks

                Comment

                Working...