How do I force a task cancellation?

前端 未结 2 2052
礼貌的吻别
礼貌的吻别 2021-01-02 04:57

Assume, there is a task containing the following actions approximately:

Task someTask = new Task(() => {

  while(!IsCancellationRequested) {

    Do_some         


        
2条回答
  •  天涯浪人
    2021-01-02 05:24

    They long to terminate my application immediately. They dont want to wait while long action is running.

    By default, Tasks run on the ThreadPool as background tasks. You can safely exit the application without bothering about background threads.

    So as long as you want to quit your process there is no problem.

    Note that Thread.Abort() was quick but not safe. Not even for a quitting application, but then it would seldom cause a real problem.

提交回复
热议问题