【问题标题】:Canceling background task during WPF XBAP application exit在 WPF XBAP 应用程序退出期间取消后台任务
【发布时间】:2012-09-24 04:24:02
【问题描述】:

我有一个在后台下载一些数据的 wpf xbap 应用程序。需要在应用程序退出时停止下载并刷新缓存。现在实现如下:
应用程序.xaml:

<Application Exit="ApplicationExit">

App.xaml.cs:

private void ApplicationExit(object sender, ExitEventArgs e)
{
    BackgroundImageLoader.Instance.Stop(); // target
    ViewModelLocator.Cleanup();
    FileCacheHelpers.FlushTemporaryFolder();
}

BackgroundImageLoader.cs:

// Thread-safe singleton
public sealed class BackgroundImageLoader
{
        private static volatile BackgroundImageLoader _instance;
        private static readonly object SyncRoot = new object();
        private BackgroundImageLoader() { }
        public static BackgroundImageLoader Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (_instance == null)
                            _instance = new BackgroundImageLoader();
                    }
                }
                return _instance;
            }
        }
        // other properties, omitted for conciseness
        private Task loaderTask;
        private CancellationTokenSource ts;
        public void Start()
        {
            ts = new CancellationTokenSource();
            var ct = ts.Token;
            loaderTask = Task.Factory.StartNew(() =>
            {
                 // TaskList is an array of tasks that cannot be canceled 
                 // if they were executed, so i want to actually cancel the 
                 // whole pool after last non-canceled operation was ended
                 foreach (var task in TasksList)
                 {
                      if (ct.IsCancellationRequested)
                      {
                           Debug.WriteLine("[BackgroundImageLoader] Cancel requested");
                           // wait for last operation to finish
                           Thread.Sleep(3000);
                           FileCacheHelpers.FlushTemporaryFolder();
                           break;
                      }
                      task.DoSomeHeavyWorkHere();                             
                 }
            }, ct);
        }

        public void Stop()
        {
            if (loaderTask != null)
            {
                if (loaderTask.Status == TaskStatus.Running)
                {
                    ts.Cancel();        
                }
            }
        }
    }

Stop 被调用,loaderTask 不为 null 并且 Status 正在运行,但在 ts.Cancel() IsCancellationRequested 属性未更改之后(即使在将整个任务包装在 while(true){..} 中,如 here 所述) .加载停止,但我想这要归功于自动 GC。我的刷新方法没有执行。我错过了什么? p.s.我还需要重构功能以同时在单独的线程中运行任务,但害怕副作用。任何帮助表示赞赏。提前致谢。

【问题讨论】:

    标签: c# wpf task xbap cancellation


    【解决方案1】:

    添加了一些我在处理过程中设置和检查的手动标志,并在 Start() 开始时调用 Stop(),因此行为是可以接受的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多