Example: lr_resume_transaction_instance

In the following example, lr_stop_transaction_instance and lr_resume_transaction_instance show the effect of stopping and resuming the instance.

Action() {
    int i;
    int iteration = 1000;
    char aStr[100];
    long trans_handle;
    /* The handle argument of zero indicates 
        that there is no parent transaction */
    trans_handle = lr_start_transaction_instance("Stop&Start", 0);

    /* Do part of business process. Represented
        here by loop to ensure that some 
        time passes. */
    for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);    
      //lr_log_message("%d",i);
    }
    /* Output the duration to this point 
        with transaction instance active */
    lr_output_message("First time = %f", 
        lr_get_trans_instance_duration(trans_handle));
    // Stop the transaction instance
    lr_stop_transaction_instance(trans_handle);
    /* Output the duration to this point 
        with transaction stopped */
    lr_output_message("Immediately after stop = %f", 
        lr_get_trans_instance_duration(trans_handle));
    // Ensure that some time passes
    for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);
        //lr_log_message("%d",i);
    }
    /* Output the duration to this point 
        with transaction instance still stopped but 
        more time passed. Note that time is the same.
        The time spent in the loop while the transaction
        instance was stopped is not reported. */
    lr_output_message("After stop and loop = %f", 
        lr_get_trans_instance_duration(trans_handle));
    // Resume the transaction instance
    lr_resume_transaction_instance(trans_handle);
    /* Note that with the instance resumed, 
        all the time passed since the start is 
        reported, including the time spent in the
        loop while the instance was stopped. */
    lr_output_message("After resume time = %f", 
        lr_get_trans_instance_duration(trans_handle));
    // Add time to duration
    for (i=0; i < iteration; ++i) {
        sprintf(aStr, "%d", i);
        //lr_log_message("%d",i);
}
    /* Total time reported */
    lr_output_message("After resume and loop = %f", 
        lr_get_trans_instance_duration(trans_handle));
    // End transaction instance
    lr_end_transaction_instance(trans_handle, LR_AUTO);
    return 0;
}
Output:
Action.c(14): Notify: Transaction Stop&Start started.
Action.c(26): First time = 0.609375
Action.c(31): Notify: Transaction Stop&Start stopped.
Action.c(36): Immediately after stop = 0.609375
Action.c(50): After stop and loop = 0.609375
Action.c(54): Notify: Transaction Stop&Start resumed.
Action.c(60): After resume time = 1.250000
Action.c(70): After resume and loop = 1.859375
Action.c(75): Notify: Transaction Stop&Start ended with Pass status (Duration: 1.8594).