> For the complete documentation index, see [llms.txt](https://infinitypbr.gitbook.io/magic-pig-games/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infinitypbr.gitbook.io/magic-pig-games/juicy-actions/action-executor/executing-cancelling-and-restarting-actions.md).

# Executing, Cancelling, and Restarting Actions

## Executing Actions

This page deals with executing `ActionExecutor` objects via code. Check the [Action on Events & Action Runner](/magic-pig-games/juicy-actions/action-on-events-and-action-runner.md) page on using the ready-to-use components with `ActionExecutor` objects on them.

### `Execute(this)`

Calling `Execute(this)` is the most common way to execute <mark style="color:yellow;">Actions</mark> on an `ActionExecutor`. If the executor is able to begin or restart execution, it will do so.

The method will return a `CancellationTokenSource`, which you can cache if you'd like, though this is optional and in most cases not needed.

```csharp
_cts = myExecutor.Execute(this);
```

### `ExecuteAsync()`

If you need to wait the execution's completion before doing something else, you can use the `ExecuteAsync()` method. This pattern allows a class like `ActionRunner` to cleanly stop and restart actions, check for cancellation between loops, and ensure proper cleanup.

{% hint style="info" %}
`ActionRunner` component uses this method to loop the `ActionExecutor` once it completes.
{% endhint %}

```csharp
private CancellationTokenSource _cts;

async void StartActions()
{
    // Create a cancellation token source
    _cts = new CancellationTokenSource();
        
    // Execute with cancellation control
    var result = await myExecutor.ExecuteAsync(this, _cts.Token);
        
    if (result.WasCanceled)
        Debug.Log("Actions were canceled!");
}
```

## Cancelling Actions

Once active, you can cancel an `ActionExecutor` in a few ways.&#x20;

### Async Cancellation

If you are running an `ActionExecutor` through `ExecuteAsync()` you can cancel it:

```csharp
void StopActions()
    {
        _cts?.Cancel();
        _cts?.Dispose();
        _cts = null;
    }
    
    void OnDestroy()
    {
        _cts?.Cancel();
        _cts?.Dispose();
    }
```

### Dispose of the `CancellationTokenSource`

If you did cache the `CancellationTokenSource` when calling `Execute(this)`, you can cancel and dispose of it:

```csharp
void StopActions()
{
    if (_cts != null)
    {
        _cts.Cancel();
        _cts.Dispose();
        _cts = null;
    }
}
```

### Cancel All Actions

You can also call `CancelAllActions()` which calls `OnCancel()` on all tracked <mark style="color:yellow;">Actions</mark>. In most cases, this will stop the <mark style="color:yellow;">Actions</mark> immediately, though each <mark style="color:yellow;">Action</mark> may have different `OnCancel()` behavior.

```csharp
myExecutor.CancelAllActions();
```

### Emergency Shutdown

Generally used `OnDestroy()`, this will immediately shut down lifecycle processes, coroutines, and tracked actions.

{% hint style="danger" %}
This is only intended to be used when destroying an object or exiting playmode. Use the other methods in most cases!
{% endhint %}

```csharp
void OnDestroy() => myExecutor.EmergencyShutdown();
```

### `StopActionSequenceAction`

There's also an `Action` which stops all actions! Utilize this in an `ActionExecutor` flow to stop current sequences (on "this" `ActionExecutor`), all sequences (globally), or sequences on another specific target.

## Restarting Actions

If the `ActionExecutor` allows restarts, and if the cooldown period has elapsed, then you can trigger a restart even if the actions have not yet completed.

<figure><img src="/files/n9eNzEZbEPfMRBhYHy34" alt="" width="563"><figcaption></figcaption></figure>

Simply call `Executor.RestartActions()` to do this.

{% hint style="success" %} <mark style="color:yellow;">Actions</mark> can have special logic that is triggered via `RestartActions()`, and new instances of currently running actions can carry over cached values. `ScaleAction`, for example, will cache the `baseScale` and new instances created via `OnRestart()` will utilize the original cached value rather than saving the current — and perhaps mid-animation — value as a new base.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/juicy-actions/action-executor/executing-cancelling-and-restarting-actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
