c# - How to remove duplicate items from a queue within a time frame? -


i remove duplicate entries queue in efficient way. queue has custom class datetime , fullpath , few other things

private queue<mycustomclass> sharedqueue; 

the datetime in class timestamp when inserted queue. logic use following: remove duplicates queue if fullpath identical within 4 second window (i.e. if added queue within 4 seconds of duplicate fullpath). have events want watch few duplicates still arrive , ok.

i using c# 2.0 , filesystemwatcher class , worker queue.

there bunch of ways this: trim queue each time item added it, or when working on queue skip processing of current duplicate item.

or should use 'global private' variable dictionary< string, datetime> ? can search it? or local copy of queue ? perhaps best limit local queue 100 items in case of many file events? though in case 'should be' relatively few files monitor in folder... things change...

thanks help.

:edit: feb 10 8:54 est: decided implement simple solution far can tell. don't think holding on dict keys long...

:edit: feb 10 9:53 est: updated dictionary cannot contain duplicate values.

   public void queueinput(hotsynchunit.rcdfswfile rcd) // start worker thread when program starts. // call terminate.set() in programs exit routine or close handler etc. {   // lock shared queue   lock (sharedqueue)   {     if (!isduplicatequeueinput(rcd))  // add unique values queue     {       sharedqueue.enqueue(rcd);       somethingtodo.set();     }   } } // public void queueinput  private bool isduplicatequeueinput(hotsynchunit.rcdfswfile rcd) /* return true if object duplicate object.  * pseudo code:  *   * isduplicate = false  * lock dictionary  * -if lasttimestamp > 4 seconds ago       // optimization: save lasttimestamp  *    if dict.count > 0 clear dictionary  *    return isduplicate  * -if not dict.trygetvalue(spath, dttimestamp)  *    dict.addkey()  * -else  *    compare key timestamp currenttime  *    if key timestamp <= 4 seconds ago  *       isduplicate = true  *  *    dict.removekey()  *    dict.addkey()  *   * return isduplicate */ {   // put real code here } 

i thought using any collection similar generic hashtable... this:

dictionary<string, yourclass> dict = new dictionary<string, yourclass>();  /// let's assume want add/check "c:\demo.txt"  if (!dict.containskey(@"c:\demo.txt")) {    /// add items dict passing fullpath key , objects value    dict.add(@"c:\demo.txt", obj1); }  else if (dict[@"c:\demo.txt"].checkforintervall()) {    /// replace current object in dictionary new object - in case want to..    /// or want  } 

edit - custom class may have functionality this:

class yourcustomclass {     private datetime creationtime;      public datetime creationtime     { { return creationtime; } }      public yourcustomclass(parametersgoeshere xyz)     {           creationtime = datetime.now;     }      /// in case method return true     /// if timespan between object , otherobject     /// greater 4 seconds     public bool checkforinterval(yourcustomclass otherobject)     {          timespan diff = otherobj.creationtime.subtract(creationtime);           /// may replace 4 through other digit, or better take          /// const/global var/static ...          return diff.totalseconds > 4;     }      /// other stuff need ... } 

of course loose functionality of queue - massive increase in runtime if queue containts many elements.

hth


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -