zoukankan      html  css  js  c++  java
  • Tasks in parallel

    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:UsersxxxxxxxxxxDocumentsVisual Studio 2008projectsIntegration Services Project1Integration Services Project1Package.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:Templog.txt", string.Format("{0} {1} ", DateTime.Now, isFailed ? "Failed!" : "Suceed!"));
    
                };
    
            act.BeginInvoke(null, null); //for better, have callback method...
    
        }
    }
    
  • 相关阅读:
    listview删除item
    标准listview加图标布局
    android事件消费机制,从外传到里面,里面具有优先选择权,如果里面的不需要,则传递给外面一层消费
    listview 按最新数据展示
    给listview添加数据,添加数据之后即刻显示出来,并把数据放在listview列表的最前面
    EditText限制输入长度和限定输入数字
    josn解析常见的几种方法
    bnu Robots on a grid
    hdu 1348 Wall
    hdu poj Oulipo
  • 原文地址:https://www.cnblogs.com/silva/p/3871127.html
Copyright © 2011-2022 走看看