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:\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...

    }
}
时间: 2024-11-09 02:40:47

Tasks in parallel的相关文章

并行编程多线程之Parallel

1.简介 随着多核时代的到来,并行开发越来越展示出它的强大威力!使用并行程序,充分的利用系统资源,提高程序的性能.在.net 4.0中,微软给我们提供了一个新的命名空间:System.Threading.Tasks. 2.测试类 using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using

Why you should use async tasks in .NET 4.5 and Entity Framework 6

Improve response times and handle more users with parallel processing Building a web application using non blocking calls to the data layer is a great way to increase the scalability of your system. Performing a task asynchronously frees up the worke

Android 下的 bolts tasks

Android 下的 bolts tasks 本文主要介绍的是在Android 下使用 bolts tasks, bolts tasks 可以很方便的让我们将一些异步task关联起来执行.让这些tasks有顺序的执行(当我们一个task的执行要基于另一个task的时候). 1. github 地址 Bolts-Android 2. gradle中引用 dependencies { compile 'com.parse.bolts:bolts-android:1.2.0' } 3. 具体的使用 (

scrapy之parallel

Limiting Parallelism jcalderone May 22nd, 2006 This blog has moved! Read this post and its comments at its new home. Concurrency can be a great way to speed things up, but what happens when you have too much concurrency? Overloading a system or a net

一箭N雕:多任务深度学习实战

1.多任务学习导引 多任务学习是机器学习中的一个分支,按1997年综述论文Multi-task Learning一文的定义:Multitask Learning (MTL) is an inductive transfer mechanism whose principle goal is to improve generalization performance. MTL improves generalization by leveraging the domain-specific inf

源码分析--AsyncTask

查看文档 AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask使适当的和易用的用户界面线程.这个类允许执行后台操作,在UI线程上发布的结果

Java 8新特性

现在,是时候把所有Java8的重要特性收集整理成一篇单独的文章了,希望这篇文章能给你带来阅读上的乐趣.开始吧! 目录结构 介绍 Java语言的新特性 2.1 Lambdas表达式与Functional接口 2.2 接口的默认与静态方法 2.3 方法引用 2.4 重复注解 2.5 更好的类型推测机制 2.6 扩展注解的支持 Java编译器的新特性 3.1 参数名字 Java 类库的新特性 4.1 Optional 4.2 Streams 4.3 Date/Time API (JSR 310) 4.

Understanding node.js by Felix Geisend&#246;rfer(翻译 by shangyan)

9/4/10 by Felix Geisendörfer Node.js has generally caused two reactions in people I've introduced it to. Basically people either "got it" right away, or they ended up being very confused. If you have been in the second group so far, here is my a

Database Sharding, The “Shared-Nothing” Approach DATABASE SHARDING

w将单个服务器上的单个数据库打碎为多个服务器上的单个数据库 http://www.agildata.com/database-sharding/ Database Sharding provides a method for scalability across independent servers, each with their own CPU, memory and disk. Contrasted with other traditional methods of achieving