0

I'm beginner in the use of task in C#, I have this method :

  public async void GetAnimateur(Action<List<fsign_fiche_signaletique>, Exception> callback)
        {
            try
            {
                Task<List<fsign_fiche_signaletique>> data = (Task<List<fsign_fiche_signaletique>>)Task.Run(
                    () =>
                    {
                        DataEntities _db = new DataEntities();
                        _db.Configuration.LazyLoadingEnabled = false;
                        var dpcs = _db.fsign_fiche_signaletique.ToList();
                        return new List<fsign_fiche_signaletique>(dpcs);
                    });
                var result = await data;
                callback(result, null);
            }
            catch (Exception ex)
            {
                callback(null, ex);
            } 
        }

I need to know how can I stop the execution of the task and kill its threads after the finish of the handling.

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 3
    Just FYI, you're only [supposed to use async void in very specific situations](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx). Also, what do you mean by kill its threads? The task should handle all that for you (if it uses a new thread at all, it might not). What are you trying to achieve? – George Mauer Feb 12 '15 at 15:00
  • 1
    The TPL will take care of returning the thread to the threadpool for you. There's no need to explicitly terminate anything. – Yuval Itzchakov Feb 12 '15 at 15:07
  • @GeorgeMauer I just need that the task will be executed only one time – Lamloumi Afif Feb 12 '15 at 15:08
  • This method shouldn't accept a callback, rather it should return a `Task`. That's the whole design of the TPL. – Servy Feb 12 '15 at 15:17

2 Answers2

2

There is no way to stop the execution of the Task after it has already finished because, by definition, it has already stopped executing.

Task.Run will return the Thread Pool thread back to the thread pool, if the pool was used, or allow the allocated thread to finish and tear itself down if a new thread was used.

There is nothing for you to do explicitly.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Or run immediately, synchronously without creating a new thread at all. That is also possible :) – George Mauer Feb 12 '15 at 15:19
  • @GeorgeMauer Presumably the operation is asynchronous for good reason, and it's important that it not run synchronously. Of course, it could perform the operation asynchronously without using a new thread. – Servy Feb 12 '15 at 15:21
1

You can use a CancellationToken for that and then in your task's code periodically check for it. See e.g.:

But it looks like your code is not especially well suited for that. To cancel a task, it should be possible to separate it into different steps, which isn't the case here.

If your thinking more of aborting the task (maybe because of db connection issues), then you should simply give the task an appropriate timeout or - even simplier - return from it as soon as you encounter an error.

EDIT: Or did I get you wrong and you want to abort the task after its execution? For that, you'll need to invent a time machine - if you succeed, let me know ;-)...

Community
  • 1
  • 1
Thomas Weller
  • 11,631
  • 3
  • 26
  • 34