BeginCancel

zeinab mokdad 1 Reputation point
2021-09-01T15:43:45.11+00:00

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,275 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-09-02T06:34:18.43+00:00

    @zeinab mokdad , 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.

    1 person found this answer helpful.
    0 comments No comments