Task

From GTA Network Wiki
Jump to: navigation, search

Task
Server-Side Function

utilities-terminal.png Server-Side csharp.png C#

Runs thread-safe API calls on asynchronous threads.

This can also be just used to delay certain tasks.

Syntax

void NAPI.Task.Run(Action task, long delayTime = 0);

Optional:

  • delayTime: parameter input should be in long type, default value: 0,
  • Param description: delay time in milliseconds.

Usage example(s)

Non-Threaded

  • Delaying task invocation
NAPI.Task.Run(() => 
{ 
    // Some code
}, delayTime: 2000);

Threaded

  • Non-delayed task invocation
System.Threading.Tasks.Task.Run(() => 
{ 
    NAPI.Task.Run(() => 
    { 
        // Some non-thread safe API methods
    }); 
});

  • Delaying task invocation (non thread blocking)
System.Threading.Tasks.Task.Run(() => 
{ 
    NAPI.Task.Run(() => 
    { 
        // Some non-thread safe API methods
    }, delayTime: 2000); // delay time in ms
});

  • Delaying task invocation (async thread blocking)
System.Threading.Tasks.Task.Run(() => 
{ 
    await System.Threading.Tasks.Task.Delay(2000); // Blocks the thread for 2 seconds before running the rest of the code
    NAPI.Task.Run(() => 
    { 
        // Some non-thread safe API methods
    });
});

Note

System.Threading.Tasks could be simplified by adding the following at the top of your code

using System.Threading.Tasks;

Changelog

Version Description
1.0 Release.

References