码迷,mamicode.com
首页 > 其他好文 > 详细

Tasks in parallel

时间:2014-07-27 21:40:05      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using Microsoft.SqlServer.Dts.Runtime;



struct Response
    {
        public bool DTSResult { get; set; }
        public string Message { get; set; }
    }


public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    Response ExecuteWorkA()
    {
        bool flagA = false;

        try
        {
            //Thread.Sleep(16000);
            string pkgLocation;
            Package pkg;
            Application app;
            DTSExecResult pkgResults;

            pkgLocation =
              @"C:\Users\xxxxxxxxxx\Documents\Visual Studio 2008\projects\Integration Services Project1\Integration Services Project1\Package.dtsx";
            app = new Application();
            pkg = app.LoadPackage(pkgLocation, null);
            pkgResults = pkg.Execute();
            flagA = pkgResults == DTSExecResult.Success;

            //throw new Exception("err");
        }
        catch (Exception ex)
        {
            flagA = false;
        }
        return new Response { DTSResult = flagA };
    }


    Response ExecuteWorkB()
    {
        bool flagB = false;
        try
        {
            Thread.Sleep(14000);
            flagB = true;
            //throw new Exception("error");
        }
        catch (Exception ex)
        {
            flagB = false;
        }
        return new Response { DTSResult = flagB };
    }


    Response ExecuteWorkC()
    {
        bool flagC = false;
        Thread.Sleep(17000);
        flagC = true;
        return new Response { DTSResult = flagC};
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<Func<Response>> lst = new List<Func<Response>>();
        lst.Add(ExecuteWorkA);
        lst.Add(ExecuteWorkB);
        lst.Add(ExecuteWorkC);


        List<Response> result = new List<Response>();

        Action act = () =>
            {

                Parallel.ForEach(lst, (func) =>
                {
                    result.Add(func());
                });

                bool isFailed = false;
                foreach (Response item in result)
                {
                    if (!item.DTSResult)
                    {
                        isFailed = true;
                        break;
                    }
                }
                
                if(result.Count > 0 )
                    File.AppendAllText(@"D:\Temp\log.txt", string.Format("{0} {1} ", DateTime.Now, isFailed ? "Failed!" : "Suceed!"));

            };

        act.BeginInvoke(null, null); //for better, have callback method...

    }
}

 

Tasks in parallel

标签:

原文地址:http://www.cnblogs.com/silva/p/3871127.html

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