2012-11-07 55 views
5

我知道,使用.net 4.5時,有一些await異步關鍵字可以方便地調用異步方法。我目前正在研究如何在C#4.0中進行異步調用。我想要的一個例子是在datagrid被數據綁定的地方進行異步調用。如何在asp.net C#4.0中調用異步方法?

如果你能給我提供一些鏈接,我會非常感激。

回答

7

看看使用任務,這是在.Net 4中可用,並應該幫助你。一個簡單的例子可能是這樣的:

 public void MainFlow() 
    { 
     Task taskWork = Task.Factory.StartNew(new Action(DoWork)); 

     //Do other work 


     //Then wait for thread finish 
     taskWork.Wait(); 
    } 


    private void DoWork() 
    { 
     //Do work 
    } 

更多,這裏有一個看看

http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=vs.100).aspx

+0

Task.Run在.Net 4.0中不存在。 – JPProgrammer

+0

使用Task.Factory.StartNew for .Net 4 –

1

我們做了一些工具類執行異步操作。

什麼這些方法真正做到: 異步執行的動作,然後相應職位completition執行的同步上下文。

當我們稱之爲長時間運行操作時,我們將它作爲TPL的包裝來輕鬆地使用它在WPF中。這個班級也有類似的行動可以取消,而不是放棄。

public static class AsyncExecutor 
    { 
     public static CancellationTokenSource ExecuteBlockingOperation(Action action, Action completition, Action<AggregateException> onException) 
     { 
      if (action == null) 
       throw new ArgumentNullException("action"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task(action, TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
            { 
             if (!token.IsCancellationRequested) 
              completition(); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 

     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn>(Action<TIn> action, TIn parameter, Action<TIn> completition, Action<AggregateException, TIn> onException) 
     { 
      if (action == null) 
       throw new ArgumentNullException("action"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task(() => action(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
            { 
             if (!token.IsCancellationRequested) 
              completition(parameter); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 

     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TOut>(Func<TOut> func, Action<TOut> completition, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(func, TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
            { 
             if (!token.IsCancellationRequested) 
              completition(asyncPart.Result); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 

     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TOut, TIn> completition, Action<AggregateException, TIn> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(asyncPart.Result, parameter); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TOut> completition, Action<AggregateException, TIn> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(asyncPart.Result); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TIn> completition, Action<AggregateException, TIn> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(parameter); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action completition, Action<AggregateException, TIn> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TIn> completition, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(parameter); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TOut, TIn> completition, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(asyncPart.Result, parameter); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TOut> completition, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(asyncPart.Result); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static CancellationTokenSource ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action completition, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var cts = new CancellationTokenSource(); 
      var token = cts.Token; 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
      { 
       if (!token.IsCancellationRequested) 
        completition(); 
      }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 

      return cts; 
     } 

     public static void ExecuteBlockingOperation(Action action, Action completition, Func<bool> shouldComplete, Action<AggregateException> onException) 
     { 
      if (action == null) 
       throw new ArgumentNullException("action"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task(action, TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
            { 
             if (shouldComplete == null || shouldComplete()) 
              completition(); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 
     } 

     public static void ExecuteBlockingOperation<TIn>(Action<TIn> action, TIn parameter, Action<TIn> completition, Predicate<TIn> shouldComplete, Action<AggregateException, TIn> onException) 
     { 
      if (action == null) 
       throw new ArgumentNullException("action"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task(() => action(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPart => 
            { 
             if (shouldComplete == null || shouldComplete(parameter)) 
              completition(parameter); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 
     } 

     public static void ExecuteBlockingOperation<TOut>(Func<TOut> func, Action<TOut> completition, Predicate<TOut> shoudComplete, Action<AggregateException> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(func, TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); 
      task.ContinueWith(asyncPartTask => 
            { 
             if (shoudComplete == null || shoudComplete(asyncPartTask.Result)) 
              completition(asyncPartTask.Result); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 
     } 

     public static void ExecuteBlockingOperation<TIn, TOut>(Func<TIn, TOut> func, TIn parameter, Action<TOut, TIn> completition, Func<TOut, TIn, bool> shouldComplete, Action<AggregateException, TIn> onException) 
     { 
      if (func == null) 
       throw new ArgumentNullException("func"); 

      if (completition == null) 
       throw new ArgumentNullException("completition"); 

      var scheduler = TaskScheduler.FromCurrentSynchronizationContext(); 

      var task = new Task<TOut>(() => func(parameter), TaskCreationOptions.LongRunning); 
      task.ContinueWith(asyncPartTask => onException(asyncPartTask.Exception, parameter), CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, scheduler); // on Exception 
      task.ContinueWith(asyncPart => 
            { 
             if (shouldComplete == null || shouldComplete(asyncPart.Result, parameter)) 
              completition(asyncPart.Result, parameter); 
            }, 
           CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler); 
      task.Start(); 
     }    
    }