Namespace Tizen.Core
Tizen Core (TCore) is a new main loop model that provides per thread loops, message and event transmission, uniquely named threads.
Overview
Tizen Core (TCore) is a new main loop that improves the existing main loop model. It supports creating and running the main loop in multiple threads. Tizen Core provides an API that supports secure communication between threads.
Preparation
To use the Tizen Core API, you must define using Tizen.Core
, as shown below:
using Tizen.Core;
Initializing Tizen Core
Before using Tizen Core, Calls TizenCore.Initialize() as shown below:
TizenCore.Initialize();
Shutting down Tizen Core
When Tizen Core is no longer needed, shut down Tizen Core with the code below:
TizenCore.Shutdown();
Managing Tizen Core tasks
This section will cover creating, executing, and terminating the CoreTask objects, It will also cover adding idle jobs and timers to the main loop.
Creating a task
Here` an example on how to create a Task object:
{
TizenCore.Initialize();
var task = new Task("task1");
}
task
was created with the name "task1" and configured to use its own thread. The created task
instance should be removed using Dispose()
method when it is no longer needed.
Running a task
In this example, we'll cover the code to execute a previously created task:
{
task.Run();
}
task
creates and runs a thread named "task1". After calling Run()
method, the thread is created and the loop starts running.
Checking if a task is running
An example of checking if a task is running using Running
property:
{
if (task.Running)
{
Log.Debug(LogTag, "task1 is running");
}
else
{
Log.Debug(LogTag, "task1 is not running");
}
}
Adding an timer job to Tizen Core
Here's an example of registering a timer that calls the handler every 100 ms.
{
var timerId = task.AddTimer(100, () => {
Log.Debug(LogTag, "timer handler is invoked");
return true;
});
}
Adding an action to Tizen Core
Let'w write an example that adds an action to Task
.
{
var task = TizenCore.Find("Test") ?? TizenCore.Spawn("Test");
task.Post(() => {
Log.Debug(LogTag, "Action is invoked");
});
}
Managing Tizen Core channels
Tizen Core channel Provides a communication channel that allows safe sending and receiving of data between threads. This channel can be used to exchange information in a synchronized state without data conflict. This section describes how to create a channel sender and receiver pair, send and receive data, and destroy the channel sender and receiver pair.
Creating a channel
Here'a an example on how to create a Channel object:
{
try {
var channel = new Channel();
}
catch (OutOfMemoryException)
{
Log.Error(LogTag, "Exception occurs");
}
}
Creating and transmitting a channel object
This example show how to create and transmit a channel object:
{
TizenCore.Initialize();
var channel = new Channel();
var receiver = channel.Receiver;
receiver.Received += (s, e) => {
Log.Debug(LogTag, "OnChannelObjectReceived. Message = {}", (string)e.Data);
};
var task = TizenCore.Find("ReceivingTask") ?? TizenCore.Spawn("ReceivingTask");
task.AddChannelReceiver(receiver);
var sender = channel.Sender;
string message = "Test message";
using (var channelObject = new ChannelObject(1, message))
{
sender.Send(channelObject);
}
}
The example shows adding a receiver to a ReceivingTask and delivering a message using a ChannelSender. The channel event is passed to the main loop of the ReceivingTask, causing the event handler to be called.
Managing Tizen Core events
A feature to deliver events to specific tasks, which can be used to wait for completion of tasks or send notifications to other threads. This section covers creating events, registering event handlers with them, attaching them to the main loop, and receiving events.
Creating an event and registering an event handler
Here's an example of creating an event and registering an event handler:
{
var coreEvent = new Event();
coreEvent.EventReceived += (s, e) => {
Log.Debug(LogTag, "OnEventReceived. Message = {}", (string)e.Data);
}
}
The example shows creating an event and registering an event handler for it. The created event is added to the EventTask.
Creating an event object and delivering it to the task.
{
TizenCore.Initialize();
var coreEvent = new Event();
coreEvent.EventReceived += (s, e) => {
Log.Debug(LogTag, "OnEventReceived. Message = {}", (string)e.Data);
}
var task = TizenCore.Find("EventTask") ?? TizenCore.Spawn("EventTask");
task.AddEvent(coreEvent);
string message = "Event message";
using (var eventObject = new EventObject(1, message))
{
task.EmitEvent(eventObject);
}
}
The generated event is added to an EventTask, and a corresponding EventObject is passed to it. The event is then delivered to the main loop of the EventTask where the EventHandler is called.
Classes
Channel
The class for managing communication channels between tasks of Tizen Core.
ChannelObject
Represents a channel object used for inter-task communication.
ChannelReceivedEventArgs
Represents the arguments for the event raised when an object has been received through a channel.
ChannelReceiver
Represents the channel receiver used for inter-task communication.
ChannelSender
Represents the channel sender used for inter-task communication. It provides methods to send messages between tasks in order to facilitate task coordination.
Event
Represents the event used for broadcasting events.
EventObject
Represents an event object used for broadcasting the event.
EventReceivedEventArgs
Represents the arguments passed to the event handler when the event data is received.
Task
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.
TizenCore
The class which provides functions related to the Tizen Core.