# Run Tasks Asynchronously

To run a Task asynchronously from a script, there are two options: either the InvokeAsync or RunAsync $ functions can be used:

$System.InvokeAsync("Tasks.[TaskName].Run",new object[0]);

or

$Tasks.[TaskName].RunAsync();

The following example demonstrates how to call a (typically time-consuming) User Script asynchronously from another script using the InvokeAsync $ function. This example creates a Page with a Button that, by pressing on it, performs some time-consuming operation and then displays a message after the operation finished. The time-consuming operation and the message display is implemented in another User Script. In order to not block the UI while the lengthy operation is performed, we will call the User Script asynchronously:

1. From the Project Explorer, create the following User Script:
   - Name: MyTimeConsuminScriptWithOneParam
   - Arguments
     - Name: message
     - Type: string
2. Open the script by double-clicking on it and enter the following into the Script Editor:

       //Let the time-consuming operation be a Sleep for 2 seconds
       System.Threading.Thread.Sleep(2000);

       //Show the message from the "message" string argument
       $HMI.ShowMessage(message);

3. Create a Page and place a Button on it.
4. Right-click the button and select Scripts > On Mouse Click.
5. Enter the following script:

       //Call my user script asynchronously with one argument that is the "End of script." message.

       $System.InvokeAsync("MyTimeConsuminScriptWithOneParam", new Object[1] {"End of script."});