使用Task进行多次查询的结果取舍

Demo115

使用Task.StartNew(...访问服务...).ContinueWith(...更新控件...)模式处理耗时查询并绑定结果再合适不过,但查询时间的不确定性使得需要做更多工作控制结果取舍以正确更新控件。

比如说一张分月的报表窗体,用户可以使用形如“下个月”的按钮进行查询。现在是1月,用户快速点击“下个月”两次,可能3月份的数据少,查询结果迅速返回并反映在窗体上,但2月份的数据慢悠悠返回时,延续任务将同样更新控件,控件 最终展示2月份数据,而我们已经不感兴趣。

对其处理同样十分简单,可以考虑限制查询频率,如一次查询未完成时禁用下一次查询,另外的解决方法是只执行最后一个任务的延续任务,更复杂的情况是对任务使用CancellationToken掌握更多的控制权。本文示例第二种方法。

Task使用一个递增的可空整型字段标记自己,注意这个字段在Task外部访问将返回Null。使用外部声明的整型变量缓存这个ID,由于后续任务可以获取到前置任务的引用,故只在前置任务ID等于外部变量时才进行控件更新即可。

值得注意的是,由于异步任务异常的处理需要通过访问Task.Exception属性完成,而我们选择执行延续任务,故相等判断的逻辑之后需添加异常访问代码。再因为我们只对最后一次任务感兴趣,故简单的GetBaseException()即可。

示例代码使用一个ID标注自己,较大的Product ID必然是最近一次查询产生的。

服务类实现:

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class VirtualService
{
    private static int id = 0;
    private System.Threading.Timer timer;

    public VirtualService()
    {
        timer = new System.Threading.Timer(state => increase(), null, 100, 100);
    }

    private void increase()
    {
        System.Threading.Interlocked.Increment(ref id);
    }

    public static void Reset()
    {
        id = 0;
    }

    public List<Product> GetProducts()
    {
        List<Product> products = new List<Product>();
        for (int i = 0; i < 10; i++)
        {
            Product product = new Product();
            product.Name = "Product " + id;
            product.Id = id;
            products.Add(product);
        }

        Thread.Sleep(new Random().Next(1, 3000));
        if (DateTime.Now.Millisecond % 3 == 0)
        {
            throw new Exception("some error");
        }
        return products;
    }
}

设计窗体:

namespace FormTest.UnpredictableTaskFolder
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing"<true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // treeView1
            // 
            this.treeView1.Location = new System.Drawing.Point(12, 12);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(173, 413);
            this.treeView1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(424, 431);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "New Task";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(505, 431);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Clear";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(197, 12);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(383, 149);
            this.textBox1.TabIndex = 3;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(197, 167);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox2.Size = new System.Drawing.Size(383, 91);
            this.textBox2.TabIndex = 3;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(197, 264);
            this.textBox3.Multiline = true;
            this.textBox3.Name = "textBox3";
            this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox3.Size = new System.Drawing.Size(383, 91);
            this.textBox3.TabIndex = 4;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(197, 361);
            this.textBox4.Multiline = true;
            this.textBox4.Name = "textBox4";
            this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox4.Size = new System.Drawing.Size(383, 64);
            this.textBox4.TabIndex = 4;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(592, 466);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.treeView1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TreeView treeView1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox4;
    }
}

逻辑:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;

namespace FormTest.UnpredictableTaskFolder
{
    public partial class Form1 : Form
    {
        private int currentTaskId;
        private AsyncOperation opr;

        public Form1()
        {
            InitializeComponent();
            opr = AsyncOperationManager.CreateOperation(null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            VirtualService service = new VirtualService();
            DateTime now = DateTime.Now;

            Task.Factory
                .StartNew<List<Product>>(() =>
                {
                    currentTaskId = Task.CurrentId.Value;
                    opr.Post(obj => textBox1.AppendText(String.Format("Start: TaskID {0,-3} {1}.{2}{3}", currentTaskId, now.ToLongTimeString(), now.Millisecond, Environment.NewLine)), null);
                    return service.GetProducts();
                })
                .ContinueWith(t =>
                {
                    textBox2.AppendText(String.Format("Continue: TaskID {0,-3} Time {1,6:F2}{2}", t.Id, (DateTime.Now - now).TotalMilliseconds, Environment.NewLine));

                    if (t.Id == currentTaskId)
                    {
                        button2.Enabled = true;
                        if (t.Exception == null)
                        {
                            List<Product> products = t.Result;
                            textBox3.AppendText(String.Format("Tree: TaskID {0,-3} ProductID {1,-4}{2}", t.Id, products.First().Id, Environment.NewLine));
                            if (t.Id == currentTaskId)
                            {
                                buildTree(t.Result);
                            }
                        }
                        else
                        {
                            t.Exception.Handle(ex =>
                            {
                                textBox4.AppendText(String.Format("Exception: TaskID {0,-3} {1}{2}", t.Id, ex.Message, Environment.NewLine));
                                return true;
                            });
                        }
                    }

                    if (t.Exception != null)
                    {
                        t.Exception.GetBaseException();
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
        }

        private void buildTree(List<Product> products)
        {
            treeView1.Nodes.Clear();
            foreach (var product in products)
            {
                treeView1.Nodes.Add(new TreeNode(product.Name));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            VirtualService.Reset();
            treeView1.Nodes.Clear();
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
        }
    }
}
原文地址:https://www.cnblogs.com/Jusfr/p/2538620.html