Class Task

Definition

Namespace:
Tizen.Core
Assembly:
Tizen.Core.dll

Represents the task used for creating, running and terminating the thread with the main loop. The state of the task can be changed as follows: Constructed -> Running -> Terminated. To start the task, use 'Task.Run()' method. Once started, the task enters into 'Running' state. To terminate the task, use 'Task.Quit()' method. After termination, the task returns back to 'Constructed' state.

C#
Copy
public class Task : IDisposable
Inheritance
object
Task
Implements
System.IDisposable

Constructors

View Source

Task(string)

Initializes the Task class with the specified ID.

Declaration
C#
Copy
public Task(string id)
Parameters
Type Name Description
string id

The unique identifier for the task.

Remarks

The constructor throws an exception when the ID already exists. By default, the task creates a separate thread. However, if the id is set to "main", no separate thread is created. In such case, the 'main' task will operate on the main application thread instead.

Examples
Copy
TizenCore.Initialize(); var task = new Task("Worker"); task.Run();
Exceptions
Type Condition
System.ArgumentException

Thrown when the id is invalid or a Task with that ID already exists.

System.OutOfMemoryException

Thrown when out of memory.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

Properties

View Source

Running

Checks whether the task is running or not.

Declaration
C#
Copy
public bool Running { get; }
Property Value
Type Description
bool

Methods

View Source

AddChannelReceiver(ChannelReceiver)

Adds a channel receiver to a main loop of the task.

Declaration
C#
Copy
public void AddChannelReceiver(ChannelReceiver receiver)
Parameters
Type Name Description
ChannelReceiver receiver

The channel receiver instance that needs to be added.

Examples

In the following code snippet, we create a channel, find or spawn a task named "ReceivingTask", and then add the channel receiver to the task's main loop by calling the 'AddChannelReceiver' method.

Copy
var channel = new Channel(); var task = TizenCore.Find("ReceivingTask") ?? TizenCore.Spawn("ReceivingTask"); var receiver = channel.Receiver; receiver.Received += (sender, args) => { Console.WriteLine("OnChannelMessageReceived. Message = " + (string)args.Data); }; task.AddChannelReceiver(receiver);
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the argument is null.

System.ArgumentException

Thrown when the argument is invalid.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

System.OutOfMemoryException

Thrown when out of memory.

View Source

AddEvent(Event)

Adds an event to a main loop of the task. If the event is successfully added, its unique identifier is assigned to the event. The identifier can then be used later to identify the specific event among others.

Declaration
C#
Copy
public void AddEvent(Event coreEvent)
Parameters
Type Name Description
Event coreEvent

The event instance.

Remarks

This method allows you to associate an event with a specific task. By adding an event to a task's main loop, other threads can utilize this event to communicate with the task. However, note that once an event is attached to a task, it cannot be reused or attached to another task. If the argument passed to this method is null, an exception will be thrown. Additionally, if the event has been previously added, an argument exception will be raised.

Examples
Copy
var coreEvent = new Event(); coreEvent.EventReceived += (sender, args) => { Console.WriteLine("OnEventReceived. ID = {}, Message = {}", args.Id, (string)args.Data); }; var task = TizenCore.Find("EventTask") ?? TizenCore.Spawn("EventTask"); task.AddEvent(coreEvent);
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the argument is null.

System.ArgumentException

Thrown when the argument is invalid.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

System.OutOfMemoryException

Thrown when out of memory.

View Source

AddTimer(uint, Func<bool>)

Adds a recurring timer to a main loop of the task.

Declaration
C#
Copy
public int AddTimer(uint interval, Func<bool> callback)
Parameters
Type Name Description
uint interval

The interval of the timer in milliseconds.

System.Func<TResult><bool> callback

The recurring timer callback function which returns whether or not to continue triggering the timer.

Returns
Type Description
int

The registered timer ID to be used with RemoveTimer(int).

Remarks

The callback function will be called every time the specified interval elapses. It should return true to keep the timer running, otherwise the timer will be stopped.

Examples
Copy
var task = TizenCore.Find("TimerTask") ?? TizenCore.Spawn("TimerTask"); var timerId = task.AddTimer(1000, () => { Console.WriteLine("Timer callback is invoked"); return true; });
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the callback argument is null.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

System.OutOfMemoryException

Thrown when out of memory.

View Source

Dispose()

Release any unmanaged resources used by this object.

Declaration
C#
Copy
public void Dispose()
View Source

Dispose(bool)

Release any unmanaged resources used by this object.

Declaration
C#
Copy
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
bool disposing

If true, disposes any disposable objects. If false, does not dispose disposable objects.

View Source

EmitEvent(EventObject)

Emits the event object to all registered event handlers of the task. It's similar to Event.Emit(), but EmitAllEvent() sends the event object to every event handler of the task while Event.Emit() sends the event object only to the target event's event handler.

Declaration
C#
Copy
public void EmitEvent(EventObject eventObject)
Parameters
Type Name Description
EventObject eventObject

The event object instance to be sent.

Examples
Copy
int id = 0; string message = "Test Event"; using (var eventObject = new TCoreEventObject(id++, message)) { var task = TizenCore.Find("EventTask") ?? TizenCore.Spawn("EventTask"); task.EmitEvent(eventObject); }
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the argument is null.

System.ArgumentException

Thrown when the argument is invalid.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

View Source

~Task()

Finalizes the Task class.

Declaration
C#
Copy
protected ~Task()
View Source

Post(Action)

Posts an action to be executed later.

Declaration
C#
Copy
public void Post(Action action)
Parameters
Type Name Description
System.Action action

The action callback to post.

Remarks

The action callback will be executed by the main loop of the task. If there was any error during this process, an appropriate exception will be thrown. In order to prevent the action from throwing an exception, you should add a try/catch block. If not, it may cause the application to crash or terminate.

Examples
Copy
var task = TizenCore.Find("Test") ?? TizenCore.Spawn("Test"); task.Post(() => { Console.WriteLine("Test task"); });
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the action argument is null.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

System.OutOfMemoryException

Thrown when out of memory.

View Source

Post(Func<Task>)

Posts a task to be executed later.

Declaration
C#
Copy
public void Post(Func<Task> task)
Parameters
Type Name Description
System.Func<TResult><System.Threading.Tasks.Task> task

The task to post.

Remarks

The task will be stored in the internal map using its unique identifier. Then it will be added as an idle job to the main loop of the task. If there was any error during this process, the task will be removed from the map and an appropriate exception will be thrown. In order to prevent the task from throwing an exception, you should add a try/catch block. If not, it may cause the application to crash or terminate.

Examples
Copy
var channel = new Channel(); var task = TizenCore.Find("Sender") ?? TizenCore.Spawn("Sender"); int id = 0; task.Post(async () => { var channelObject = await channel.Receiver.Receive(); var message = (string)channelObject.Data; Console.WriteLine("Received message = " + message); });
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the task argument is null.

System.InvalidOperationException

Thrown when failed because of an invalid operation.

System.OutOfMemoryException

Thrown when out of memory.

View Source

Quit()

Quits the main loop of the task.

Declaration
C#
Copy
public void Quit()
Remarks

This function can be called from any thread. It requests the task to finish the current iteration of its loop and stop running. All pending events in the event queue will be processed before quitting. Once the task quits, it's finished. To start another task, you need to create a new one and call the Run() method on it.

Examples
Copy
var coreTask = new TCoreTask("Runner"); coreTask.Run(); if (coreTask.Running) { coreTask.Quit(); }
Exceptions
Type Condition
System.InvalidOperationException

Thrown when failed because of an invalid operation.

View Source

RemoveChannelReceiver(ChannelReceiver)

Removes the registered channel receiver from the main loop of the task.

Declaration
C#
Copy
public void RemoveChannelReceiver(ChannelReceiver receiver)
Parameters
Type Name Description
ChannelReceiver receiver

The channel receiver instance.

Examples
Copy
var channel = new Channel(); var task = TizenCore.Find("ReceivingTask") ?? TizenCore.Spawn("ReceivingTask"); var receiver = channel.Receiver; receiver.Received += (sender, args) => { Console.WriteLine("OnChannelMessageReceived. Message = " + (string)args.Data); }; task.AddChannelReceiver(receiver); task.RemoveChannelReceiver(receiver);
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the argument is null.

System.ArgumentException

Thrown when the argument is invalid.

View Source

RemoveEvent(Event)

Removes the registered event from the main loop of the task.

Declaration
C#
Copy
public void RemoveEvent(Event coreEvent)
Parameters
Type Name Description
Event coreEvent

The event instance.

Examples
Copy
var coreEvent = new Event(); coreEvent.EventReceived += (sender, args) => { Console.WriteLine("OnEventReceived. ID = {}, Message = {}", args.Id, (string)args.Data); }; var task = TizenCore.Find("EventTask") ?? TizenCore.Spawn("EventTask"); task.AddEvent(coreEvent); task.RemoveEvent(coreEvent);
Exceptions
Type Condition
System.ArgumentNullException

Thrown when the argument is null.

System.ArgumentException

Thrown when the argument is invalid.

View Source

RemoveTimer(int)

Removes the registered timer from the main loop of the task. If the specified timer was already stopped, no action occurs.

Declaration
C#
Copy
public void RemoveTimer(int id)
Parameters
Type Name Description
int id

The registered timer ID.

Examples
Copy
var task = TizenCore.Find("TimerTask") ?? TizenCore.Spawn("TimerTask"); var timerId = task.AddTimer(1000, () => { Console.WriteLine("Timer handler is invoked"); return true; }); ... task.RemoveTimer(timerId);
View Source

Run()

Runs the main loop of the task.

Declaration
C#
Copy
public void Run()
Examples

Here's an example that demonstrates how to create a Core Task and run its main loop:

Copy
// Create a Core Task named "Runner" var coreTask = new TCoreTask("Runner"); // Start the main loop of the task coreTask.Run();
Exceptions
Type Condition
System.InvalidOperationException

Thrown when failed because of an invalid operation.

Implements

System.IDisposable