Jordi
Jordi

Reputation: 23257

Pending tasks pattern: try it later pattern

I'm using several FileSystemWatcher objects in order to watch several directories out.

When I do a Copy&Paste operation of several files on a folder which's watched out, the FileSystemWatcher raises an Created event for each new created file, which I capture.

I'm using a Thread Pool with a concurrency of n in order to enqueue jobs and control how many threads I want are working on it (I could create 200 files at once, however, I only want 4 threads works on it). "It" means to calculate a checksum:

private byte[] calculateChecksum(string frl)
{
    byte[] checksum = null;
    FileStream stream = null;
    try
    {
        stream = File.Open(frl, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        MD5 md5 = MD5.Create();
        checksum = md5.ComputeHash(stream);

    }
    finally
    {
        stream.Close();
    }

    return checksum;
}

The problem is that sometimes when a thread is working on that job (calculateChecksum method), it tries to get access to file, however, sometimes the file is in use (I suppouse by the SO).

I want to set a solution in order to solve this telling me: "try it later".

One solution would be, enqueue again the job on the thread pool. The problem is that I don't know how to tell later. I believe it's important, because I don't want it breaths and rest a short time and tries it again later.

For example:

I don't want that: wait 300 seconds means a thread of the thread pool is busy. It would not allow to the other enqueued jobs be dispatched.

I don't know if I've been clear. Any ideas

Upvotes: 1

Views: 239

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18387

Basically you want a retry pattern with timer.

You can do that easily with Polly:

// Retry, waiting a specified duration between each retry
Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(new[]
  {
    TimeSpan.FromSeconds(1),
    TimeSpan.FromSeconds(2),
    TimeSpan.FromSeconds(3)
  });

https://github.com/App-vNext/Polly

or like the follwing without polly:

https://stackoverflow.com/a/1563234/1384539

Upvotes: 2

Related Questions