2012-11-28 39 views
1

我在自定義代碼活動中有長時間運行的過程。如果我試圖停止建設,而這一活動運行我找回了錯誤:自定義代碼活動中的句柄取消請求

TFS215104: Failed to stop build XXXXXXXXXXXXX: it did not respond to a workflow cancellation request.

然後在生成日誌我看到:

Cannot stop the build. Details: Operation did not complete in 00:00:59.9843747.

Cannot cancel the workflow. Agent needs to be restarted. Details: The operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.

Service 'XXXXXXX - Controller': build 'XXXXXXXXXXX' was stopped. Details: Build was cancelled.

這如何取消請求,使其恢復到我的活動?我知道如何使用後臺進程,但我不知道如何捕獲我的代碼中的取消請求。

+0

FWIW a Google搜索TFS215104返回2結果。兩者都來自TFS管理角度,而不是處理編碼自定義活動。 –

回答

1

看來我讀的所有博客都顯示瞭如何使用CodeActivity而不是AsyncCodeActivity。以下是一個示例:

[BuildActivity(HostEnvironmentOption.All)] 
    public partial class SourceCodeAnalysisAsync : AsyncCodeActivity 
    { 
     private CodeActivityContext _context; 
     private bool _continue = true; 

     protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) 
     { 
      _context = context; 
      var doSomethingDelegate = new Action(DoSomething); 
      context.UserState = doSomethingDelegate; 
      return doSomethingDelegate.BeginInvoke(callback, state); 
     } 

     protected override void Cancel(AsyncCodeActivityContext context) 
     { 
      base.Cancel(context); 
      _continue = false; 
     } 

     protected override void EndExecute(AsyncCodeActivityContext context, IAsyncResult result) 
     { 
      var doSomethingDelegate = (Action)context.UserState; 
      doSomethingDelegate.EndInvoke(result); 
     } 

     private void DoSomething() 
     { 
      if (File.Exists(@"C:\test.txt")) 
      { 
       File.Delete(@"C:\test.txt"); 
      } 

      int i = 0; 
      do 
      { 
       File.AppendAllText(@"C:\test.txt", string.Format("do something {0}\r\n", i)); 
       Thread.Sleep(1000); 
       i++; 
      } while (_continue && i < 30); 
     } 
    }