Merge "[Recorder] Update descriptions to fix ths grammer errors. - Reviewed by Lionbr...
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Timer.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     using System;
21     using System.Runtime.InteropServices;
22
23     /// <summary>
24     /// Mechanism to issue simple periodic or one-shot events.<br>
25     /// Timer is provided for application developers to be able to issue
26     /// simple periodic or one-shot events.  Please note that timer
27     /// callback functions should return as soon as possible, because they
28     /// block the next SignalTick.  Please note that timer signals are not
29     /// in sync with Dali's render timer.<br>
30     /// This class is a handle class so it can be stack allocated and used
31     /// as a member.<br>
32     /// </summary>
33     public class Timer : BaseHandle
34     {
35         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
36
37         internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
38         {
39             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
40         }
41
42         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
43         {
44             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
45         }
46
47         protected override void Dispose(DisposeTypes type)
48         {
49             if(disposed)
50             {
51                 return;
52             }
53
54             if(type == DisposeTypes.Explicit)
55             {
56                 //Called by User
57                 //Release your own managed resources here.
58                 //You should release all of your own disposable objects here.
59             }
60
61             //Release your own unmanaged resources here.
62             //You should not access any managed member here except static instance.
63             //because the execution order of Finalizes is non-deterministic.
64
65             if (_timerTickCallbackDelegate != null)
66             {
67                 TickSignal().Disconnect(_timerTickCallbackDelegate);
68             }
69
70             if (swigCPtr.Handle != global::System.IntPtr.Zero)
71             {
72                 if (swigCMemOwn)
73                 {
74                     swigCMemOwn = false;
75                     NDalicPINVOKE.delete_Timer(swigCPtr);
76                 }
77                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
78             }
79
80             base.Dispose(type);
81         }
82
83
84         /// <summary>
85         /// Event arguments that passed via Tick event.
86         /// </summary>
87         public class TickEventArgs : EventArgs
88         {
89         }
90
91         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
92         private delegate bool TickCallbackDelegate(IntPtr data);
93         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
94         private TickCallbackDelegate _timerTickCallbackDelegate;
95
96         /// <summary>
97         /// brief Event for Ticked signal which can be used to subscribe/unsubscribe the event handler
98         /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>).<br>
99         /// provided by the user. Ticked signal is emitted after specified time interval.<br>
100         /// </summary>
101         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
102         {
103             add
104             {
105                 if (_timerTickEventHandler == null)
106                 {
107                     _timerTickCallbackDelegate = (OnTick);
108                     TickSignal().Connect(_timerTickCallbackDelegate);
109                 }
110                 _timerTickEventHandler += value;
111             }
112             remove
113             {
114                 _timerTickEventHandler -= value;
115                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
116                 {
117                     TickSignal().Disconnect(_timerTickCallbackDelegate);
118                 }
119             }
120         }
121
122         private bool OnTick(IntPtr data)
123         {
124             TickEventArgs e = new TickEventArgs();
125
126             if (_timerTickEventHandler != null)
127             {
128                 //here we send all data to user event handlers
129                 return _timerTickEventHandler(this, e);
130             }
131             return false;
132         }
133
134         /// <summary>
135         /// Creates a tick Timer that emits periodic signal.
136         /// </summary>
137         /// <param name="milliSec">Interval in milliseconds</param>
138         /// <returns>A new timer</returns>
139         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
140         {
141             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142
143         }
144         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
145         {
146             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
147         }
148
149
150         [Obsolete("Please do not use! this will be deprecated")]
151         public static Timer DownCast(BaseHandle handle)
152         {
153             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
154             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
155             return ret;
156         }
157
158         /// <summary>
159         /// Starts timer.<br>
160         /// In case a Timer is already running, its time is reset and timer is restarted.<br>
161         /// </summary>
162         public void Start()
163         {
164             NDalicPINVOKE.Timer_Start(swigCPtr);
165             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
166         }
167
168         /// <summary>
169         /// Stops timer.
170         /// </summary>
171         public void Stop()
172         {
173             NDalicPINVOKE.Timer_Stop(swigCPtr);
174             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
175         }
176
177         /// <summary>
178         /// Sets a new interval on the timer and starts the timer.<br>
179         /// Cancels the previous timer.<br>
180         /// </summary>
181         /// <param name="milliSec">milliSec Interval in milliseconds</param>
182         internal void SetInterval(uint milliSec)
183         {
184             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186         }
187
188         internal uint GetInterval()
189         {
190             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192             return ret;
193         }
194
195         /// <summary>
196         /// Tells whether timer is running.
197         /// </summary>
198         /// <returns>Whether Timer is started or not</returns>
199         public bool IsRunning()
200         {
201             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
202             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
203             return ret;
204         }
205
206         internal TimerSignalType TickSignal()
207         {
208             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
209             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
210             return ret;
211         }
212
213     }
214
215 }