Exemple #1
0
 /// <summary>
 /// 执行
 /// 例:ThreadHelper.Run(() => { }, (ex) => { });
 /// </summary>
 /// <param name="doWork">在线程中执行</param>
 /// <param name="errorAction">错误处理</param>
 public static System.Threading.Tasks.Task Run(Action doWork, LimitedTaskScheduler scheduler = null, Action <Exception> errorAction = null)
 {
     System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Factory.StartNew(() =>
     {
         try
         {
             if (doWork != null)
             {
                 doWork();
             }
         }
         catch (Exception ex)
         {
             if (errorAction != null)
             {
                 errorAction(ex);
             }
         }
     });
     return(task);
 }
Exemple #2
0
 /// <summary>
 /// 执行
 /// 例:ThreadHelper.Run(() => { }, (ex) => { });
 /// </summary>
 /// <param name="doWork">在线程中执行</param>
 /// <param name="errorAction">错误处理</param>
 public static System.Threading.Tasks.Task RunToLimit(Action <object> doWork, object state, LimitedTaskScheduler scheduler = null, Action <Exception> errorAction = null)
 {
     if (scheduler == null)
     {
         scheduler = _defaultScheduler;
     }
     System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Factory.StartNew((obj) =>
     {
         try
         {
             if (doWork != null)
             {
                 doWork(state);
             }
         }
         catch (Exception ex)
         {
             if (errorAction != null)
             {
                 errorAction(ex);
             }
         }
     }, state, CancellationToken.None, TaskCreationOptions.None, scheduler);
     return(task);
 }