Class Task
Definition
- 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#Copypublic class Task : IDisposable
- Inheritance
-
objectTask
- Implements
-
System.IDisposable
Constructors
Declaration
C#Copypublic 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
CopyTizenCore.Initialize(); var task = new Task("Worker"); task.Run();
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException | Thrown when the |
| System.OutOfMemoryException | Thrown when out of memory. |
| System.InvalidOperationException | Thrown when failed because of an invalid operation. |
Properties
Declaration
C#Copypublic bool Running { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
Declaration
C#Copypublic 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.
Copyvar 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. |
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#Copypublic 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
Copyvar 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. |
Declaration
C#Copypublic 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
Copyvar 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. |
Declaration
C#Copypublic void Dispose()
Declaration
C#Copyprotected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | If true, disposes any disposable objects. If false, does not dispose disposable objects. |
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#Copypublic void EmitEvent(EventObject eventObject)
Parameters
| Type | Name | Description |
|---|---|---|
| EventObject | eventObject | The event object instance to be sent. |
Examples
Copyint 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. |
Declaration
C#Copyprotected ~Task()
Declaration
C#Copypublic 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
Copyvar 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. |
Declaration
C#Copypublic 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
Copyvar 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. |
Declaration
C#Copypublic 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
Copyvar coreTask = new TCoreTask("Runner"); coreTask.Run(); if (coreTask.Running) { coreTask.Quit(); }
Exceptions
| Type | Condition |
|---|---|
| System.InvalidOperationException | Thrown when failed because of an invalid operation. |
RemoveChannelReceiver(ChannelReceiver)
Removes the registered channel receiver from the main loop of the task.
Declaration
C#Copypublic void RemoveChannelReceiver(ChannelReceiver receiver)
Parameters
| Type | Name | Description |
|---|---|---|
| ChannelReceiver | receiver | The channel receiver instance. |
Examples
Copyvar 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. |
Declaration
C#Copypublic void RemoveEvent(Event coreEvent)
Parameters
| Type | Name | Description |
|---|---|---|
| Event | coreEvent | The event instance. |
Examples
Copyvar 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. |
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#Copypublic void RemoveTimer(int id)
Parameters
| Type | Name | Description |
|---|---|---|
| int | id | The registered timer ID. |
Examples
Copyvar task = TizenCore.Find("TimerTask") ?? TizenCore.Spawn("TimerTask"); var timerId = task.AddTimer(1000, () => { Console.WriteLine("Timer handler is invoked"); return true; }); ... task.RemoveTimer(timerId);
Declaration
C#Copypublic 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. |