Question: I am looking to get the tag of a specific element in php, and I think I can do that through preg\_match, but I am not sure.

Essentially I have something like:

```
<label>Some Jazz</label>

```

What I would like to do is:

*if tag = label then do something, else do something else.*

The problem is getting the label part from the html tag. I did how ever find this [answer](https://stackoverflow.com/questions/8594165/php-preg-match-how-to-get-html-tag). How ever your thoughts are welcome.

I failed to mention I do not want the "some jazz" I want to see if the tag is a label or not. - this is in reference to some answers where it gets me whats between the tags and not the tag it's self.

Answer: 
I find the new Task-based interface to be very simple for doing things like this - even easier than using the Timer class. 

There are some small adjustments you can make to your example. Instead of:

```
task = Task.Factory.StartNew(() =>
{
    while (true)
    {
        wtoken.Token.ThrowIfCancellationRequested();
        DoWork();
        Thread.Sleep(10000);
    }
}, wtoken, TaskCreationOptions.LongRunning);

```

You can do this:

```
task = Task.Run(async () =>  // <- marked async
{
    while (true)
    {
        DoWork();
        await Task.Delay(10000, wtoken.Token); // <- await with cancellation
    }
}, wtoken.Token);

```

This way the cancellation will happen instantaneously if inside the `Task.Delay`, rather than having to wait for the `Thread.Sleep` to finish.

Also, using `Task.Delay` over `Thread.Sleep` means you aren't tying up a thread doing nothing for the duration of the sleep.

If you're able, you can also make `DoWork()` accept a cancellation token, and the cancellation will be much more responsive.
Here is what I came up with:

* Inherit from `NeverEndingTask` and override the `ExecutionCore` method with the work you want to do.
* Changing `ExecutionLoopDelayMs` allows you to adjust the time between loops e.g. if you wanted to use a backoff algorithm.
* `Start/Stop` provide a synchronous interface to start/stop task.
* `LongRunning` means you will get one dedicated thread per `NeverEndingTask`.
* This class does not allocate memory in a loop unlike the `ActionBlock` based solution above.
* The code below is sketch, not necessarily production code :)

:

```
public abstract class NeverEndingTask
{
    // Using a CTS allows NeverEndingTask to "cancel itself"
    private readonly CancellationTokenSource _cts = new CancellationTokenSource();

    protected NeverEndingTask()
    {
         TheNeverEndingTask = new Task(
            () =>
            {
                // Wait to see if we get cancelled...
                while (!_cts.Token.WaitHandle.WaitOne(ExecutionLoopDelayMs))
                {
                    // Otherwise execute our code...
                    ExecutionCore(_cts.Token);
                }
                // If we were cancelled, use the idiomatic way to terminate task
                _cts.Token.ThrowIfCancellationRequested();
            },
            _cts.Token,
            TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning);

        // Do not forget to observe faulted tasks - for NeverEndingTask faults are probably never desirable
        TheNeverEndingTask.ContinueWith(x =>
        {
            Trace.TraceError(x.Exception.InnerException.Message);
            // Log/Fire Events etc.
        }, TaskContinuationOptions.OnlyOnFaulted);

    }

    protected readonly int ExecutionLoopDelayMs = 0;
    protected Task TheNeverEndingTask;

    public void Start()
    {
       // Should throw if you try to start twice...
       TheNeverEndingTask.Start();
    }

    protected abstract void ExecutionCore(CancellationToken cancellationToken);

    public void Stop()
    {
        // This code should be reentrant...
        _cts.Cancel();
        TheNeverEndingTask.Wait();
    }
}

```