码迷,mamicode.com
首页 > Windows程序 > 详细

c#执行并行任务之Parallel与TaskFactory

时间:2015-04-17 19:50:11      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

本文转载自 http://www.cnblogs.com/icyJ/p/Parallel_TaskFactory.html
很厉害的帖子
Barrier _bar;
int _maxLength = 20, _maxChannel = 2;//同时最多2条通道,每条通道最多20个数据
bool _isCancel = false;
private void btnWrite_Click(object sender, EventArgs e)
{
    var tmpEmails = _emails.Where(x => !x.Value).Select(x => x.Key).ToList();
    var state = 0;

    _isCancel = false;
    SetControlEnable(false);
    lblProgress.Text = "* 已完成 0%";
    var channels = (tmpEmails.Count / _maxLength) + ((tmpEmails.Count % _maxLength > 0) ? 1 : 0);//总共多少条通道

    var times = (channels / _maxChannel) + ((channels % _maxChannel > 0) ? 1 : 0);//单服务器分多次
    new Action(() =>
    {
        for (int j = 0; j < times; j++)
        {
            if (_isCancel)
            {
                MessageBox.Show("任务取消!");
                break;
            }
            var currChannel = Math.Min(_maxChannel, (channels - j * _maxChannel));//两者取其小的
            _bar = new Barrier(currChannel);//根据次数设置栅栏
            var tasks = new Action[currChannel];
            for (int i = 0; i < currChannel; i++)
            {
                var subData = tmpEmails.Skip((i + j * _maxChannel) * _maxLength).Take(_maxLength).ToList();
                tasks[i] = () =>
                {
                    if (_isCancel) return;
                    var resMsg = 0;
                    Connect2WCF.RunSync(sc => resMsg = sc.UpdateMailState(subData, state));
                    if (resMsg == -1)
                        MessageBox.Show("保存失败了?详情可以查数据库日志表");
                    else if (resMsg == 0)
                        subData.ForEach(one => _emails[one] = true);//标记已经完成的。
                    new Action(() => txtEmails.Text = string.Join("\r\n", _emails.Where(x => !x.Value).Select(x => x.Key))).InvokeRun(this);
                    _bar.SignalAndWait();
                };
            }
            Parallel.Invoke(tasks);
            new Action(() => lblProgress.Text = "* 已完成 " + ((100 * (j + 1) / times)) + "%").InvokeRun(this);
        }
        new Action(() => SetControlEnable(true)).InvokeRun(this);
    }).RunThread();
}
CancellationTokenSource cts = new CancellationTokenSource();
int maxLength = 20, maxChannel = 2;//同时最多2条通道,每条通道最多20个数据
private void btnWrite_Click(object sender, EventArgs e)
{
    cts = new CancellationTokenSource();
    var tmpEmails = _emails.Where(x => !x.Value).Select(x => x.Key).ToList();
    var state = 0;

    SetControlEnable(false);
    lblProgress.Text = "* 已完成 0%";
    var channels = (tmpEmails.Count / maxLength) + ((tmpEmails.Count % maxLength > 0) ? 1 : 0);//总共多少条通道

    var times = (channels / maxChannel) + ((channels % maxChannel > 0) ? 1 : 0);//单服务器分多次
    Action<List<string>, CancellationToken> doSave = (data, ct) =>
    {
        if (ct.IsCancellationRequested) return;
        var msg = 0;
        Connect2WCF.RunSync(sc => msg = sc.UpdateMailState(data, state));
        if (msg == -1)
            MessageBox.Show("保存失败了?详情可以查数据库日志表");
        else if (msg == 0)
            data.ForEach(one => _emails[one] = true);//标记已经完成的。
        new Action(() => txtEmails.Text = string.Join("\r\n", _emails.Where(x => !x.Value).Select(x => x.Key))).InvokeRun(this);
    };

    for (int j = 0; j < times; j++)
    {
        int k = j;
        if (cts.Token.IsCancellationRequested)
        {
            MessageBox.Show("任务取消!");
            break;
        }
        var currChannel = Math.Min(maxChannel, (channels - j * maxChannel));//两者取其小的

        TaskFactory taskFactory = new TaskFactory();
        Task[] tasks = new Task[currChannel];
        for (int i = 0; i < currChannel; i++)
        {
            var subData = tmpEmails.Skip((i + j * maxChannel) * maxLength).Take(maxLength).ToList();
            tasks[i] = new Task(() => doSave(subData, cts.Token), cts.Token);
        }
        taskFactory.ContinueWhenAll(tasks,
            x => new Action(() => lblProgress.Text = "* 已完成 " + ((100 * (k + 1) / times)) + "%").InvokeRun(this), CancellationToken.None);
        Array.ForEach(tasks, x => x.Start());
    }
    SetControlEnable(true);
}

 

c#执行并行任务之Parallel与TaskFactory

标签:

原文地址:http://www.cnblogs.com/daixingqing/p/4435546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!