0

I want to use threads from ThreadPool to run same procedure at different time. Here is what I am trying to accomplish:

  1. add an item to the hash,
  2. note the time when item was created, and within X0 minutes
  3. go back and remove/do something from/with item from the hash.

From what I have read, using .Sleep() to delay execution is terrible idea. What would be better idea ?

(Unfortunately, I can't use Task Parallel Library, and limited only to .NET 3.5)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
newprint
  • 6,936
  • 13
  • 67
  • 109
  • Microsoft's Reactive Framework team did a back port of TPL for .NET 3.5 so you could use TPL. Otherwise using the reactive framework itself is a good option. Can you please provide more detail - perhaps an example - of what you're trying to do? – Enigmativity Oct 15 '14 at 03:55
  • 2
    Here's the link for details on downloading TPL for .NET 3.5 - https://www.nuget.org/packages/TaskParallelLibrary – Enigmativity Oct 15 '14 at 03:59
  • @VikasGupta I am aware of `Timer`. The only problem with them, is that I have to manage them. Nice thing about `ThreadPool` is it self contained. It would be great if there was a `TimerPool`. – newprint Oct 15 '14 at 04:07
  • @Enigmativity Great. Didn't know they backported it. Thanks you. – newprint Oct 15 '14 at 04:09

2 Answers2

1

Here's an approach using Microsoft's Reactive Framework (NuGet Rx-Main):

var query =
    Observable.Create<HashAction>(o =>
    {
        var hash = "Create Hash Somehow";
        return Observable
            .Return(new HashAction()
            {
                Action = "Add",
                Hash = hash
            })
            .Concat(
                Observable
                    .Timer(TimeSpan.FromMinutes(1.0))
                    .Select(x => new HashAction()
                    {
                        Action = "Remove",
                        Hash = hash
                    }))
            .Subscribe(o);
    });

query.Subscribe(x =>
{
    if (x.Action == "Add")
    {
        /* Add Hash */
    }
    if (x.Action == "Remove")
    {
        /* Remove Hash */
    }
});

Now, it's a bit contrived as you don't give very much concrete detail as to what you're trying to do. I don't understand what "add an item to the hash" means, let alone what "go back and remove/do something from/with item from the hash" is. A concrete example would be very useful.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

I don't know how TPL would be useful here anyway. I don't recall anything in it that involves scheduling things for future execution.

.NET includes two different basic Timer classes (in System.Timers and System.Threading), and a third one specifically for Forms (in case you're doing that). Those are the "go-to" API for this specific application.

One alternative you might consider is creating a single thread that consumes a queue of scheduled tasks, essentially implementing your own timer. In that one thread, you'd wind up using Thread.Sleep(). Yes, normally one would want to avoid that, but in a dedicated thread specifically for the purpose, it's fine. Whether you'd find that more desirable than the use of one of the Timer classes, I don't know, since I don't really understand the resistance to using one of the Timer classes.

Note that the System.Threading.Timer class has a "one-shot" mode. By passing Timeout.Infinite as the repeat interval, the timer callback is executed only once, after the initial due time interval has elapsed. The only "management" necessary is to retain a reference to the Timer instance to ensure it's not garbage-collected before the timer period elapses.

It even uses ThreadPool threads.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136