question

zeinabmokdad-7980 avatar image
0 Votes"
zeinabmokdad-7980 asked JackJJun-MSFT commented

BeginCancel

Can I have a code sample on how to use BeginCancel 'public IAsyncResult BeginCancel(TimeSpan timeout, AsyncCallback callback, object state);'
and EndCancel 'EndCancel(IAsyncResult result);' , in order to cancel a workflow application.

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@zeinabmokdad-7980, I have not heard from you for a couple of days. Please let me know if there is anything that I can help in here.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered JackJJun-MSFT edited

@zeinabmokdad-7980 , you could use the following code to use BeginCancel and EndCancel to Cancel a workflow application.

The BeginCancel method is used to Cancel a workflow instance asynchronously using the specified time-out interval, AsyncCallback, and user-provided state.

The EndCancel method determines whether a BeginCancel operation was successful. If called before the cancel operation completes, it blocks until the cancel operation is complete.

Here is a code example you could refer to.

 public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         WorkflowApplication wfApp;
         private void Form1_Load(object sender, EventArgs ev)
         {
             Activity wf = new Sequence
             {
                 Activities =
      {
          new WriteLine
          {
              Text = "Starting the workflow."
          },
          new Delay
          {
              Duration = TimeSpan.FromSeconds(20)
          },
          new WriteLine
          {
              Text = "Ending the workflow."
          }
      }
             };
    
             wfApp = new WorkflowApplication(wf);
    
             // Subscribe to any desired workflow lifecycle events.
             wfApp.Completed = delegate (WorkflowApplicationCompletedEventArgs e)
             {
                 if (e.CompletionState == ActivityInstanceState.Faulted)
                 {
                     Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
                     Console.WriteLine("Exception: {0}\n{1}",
                         e.TerminationException.GetType().FullName,
                         e.TerminationException.Message);
                 }
                 else if (e.CompletionState == ActivityInstanceState.Canceled)
                 {
                     Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
                 }
                 else
                 {
                     Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
    
                     // Outputs can be retrieved from the Outputs dictionary,
                     // keyed by argument name.
                     // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
                 }
             };
    
             wfApp.Aborted = delegate (WorkflowApplicationAbortedEventArgs e)
             {
                 // Display the exception that caused the workflow
                 // to abort.
                 Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
                 Console.WriteLine("Exception: {0}\n{1}",
                     e.Reason.GetType().FullName,
                     e.Reason.Message);
             };
    
             wfApp.Idle = delegate (WorkflowApplicationIdleEventArgs e)
             {
                 // Perform any processing that should occur
                 // when a workflow goes idle. If the workflow can persist,
                 // both Idle and PersistableIdle are called in that order.
                 Console.WriteLine("Workflow {0} Idle.", e.InstanceId);
             };
    
             wfApp.PersistableIdle = delegate (WorkflowApplicationIdleEventArgs e)
             {
                 // Instruct the runtime to persist and unload the workflow
                 return PersistableIdleAction.Unload;
             };
    
             wfApp.Unloaded = delegate (WorkflowApplicationEventArgs e)
             {
                 Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId);
             };
    
             wfApp.OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs e)
             {
                 // Display the unhandled exception.
                 Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
                     e.InstanceId, e.UnhandledException.Message);
    
                 Console.WriteLine("ExceptionSource: {0} - {1}",
                     e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);
    
                 // Instruct the runtime to terminate the workflow.
                 // Other choices are Abort and Cancel
                 return UnhandledExceptionAction.Terminate;
             };
    
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             wfApp.Run();
         }
         void CallBack(IAsyncResult iar)
         {
             Console.WriteLine("call back");
             wfApp.EndCancel(iar);
         }
         private void button2_Click(object sender, EventArgs e)
         {
             IAsyncResult result = wfApp.BeginCancel(new TimeSpan(10), CallBack, new object());
             
         }
     }

Result:
![128509-666.gif



If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.









666.gif (106.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.