方式一:endInvoke using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleApplication6 { //调用异步方法,并返回值: //方式一,采用轮训的方式; //申明一个委托; public delegate int del(int a,int b); class Program { private static int
一.概述 到目前为止,我们已经能够声明并使一个线程任务运行起来了.但是遇到一个问题:现在定义的任务都没有任何返回值,那么加入我们希望一个任务运行结束后告诉我一个结果,该结果表名任务执行成功或失败,此时该怎么办呢? 答案是使用Callable.之前定义的任务都直接实现了Runnable,该接口的run方法并无返回值.而Callable的call方法可以根据你传入的泛型参数返回对应类型的数据. 二.实现 1.实现Callable接口,定义可返回结果的线程任务 public class TaskCal