2 * Copyright(c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
19 using System.ComponentModel;
20 using System.Threading;
25 /// Mechanism to issue simple periodic or one-shot events.<br />
26 /// Timer is provided for application developers to be able to issue
27 /// simple periodic or one-shot events. Please note that the timer
28 /// callback functions should return as soon as possible because they
29 /// block the next SignalTick. Please note that timer signals are not
30 /// in sync with DALi's render timer.<br />
31 /// This class is a handle class so it can be stack allocated and used
32 /// as a member.<br />
34 /// <since_tizen> 3 </since_tizen>
35 public class Timer : BaseHandle
37 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
39 private bool played = false;
41 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
43 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
46 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
48 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
54 /// <since_tizen> 3 </since_tizen>
55 protected override void Dispose(DisposeTypes type)
62 if (type == DisposeTypes.Explicit)
65 //Release your own managed resources here.
66 //You should release all of your own disposable objects here.
69 //Release your own unmanaged resources here.
70 //You should not access any managed member here except static instance.
71 //because the execution order of Finalizes is non-deterministic.
73 if (_timerTickCallbackDelegate != null)
75 TickSignal().Disconnect(_timerTickCallbackDelegate);
78 if (swigCPtr.Handle != global::System.IntPtr.Zero)
83 NDalicPINVOKE.delete_Timer(swigCPtr);
85 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
94 /// Event arguments that passed via the tick event.
96 /// <since_tizen> 3 </since_tizen>
97 public class TickEventArgs : EventArgs
101 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
102 private delegate bool TickCallbackDelegate();
103 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
104 private TickCallbackDelegate _timerTickCallbackDelegate;
107 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
108 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
110 /// <since_tizen> 3 </since_tizen>
111 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
115 if (_timerTickEventHandler == null)
117 _timerTickCallbackDelegate = (OnTick);
118 TickSignal().Connect(_timerTickCallbackDelegate);
120 _timerTickEventHandler += value;
124 _timerTickEventHandler -= value;
125 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
127 TickSignal().Disconnect(_timerTickCallbackDelegate);
132 private bool OnTick()
134 TickEventArgs e = new TickEventArgs();
138 Tizen.Log.Fatal("NUI", $"({swigCPtr.Handle}) OnTick() is called even played is false!");
141 if (_timerTickEventHandler != null && played == true)
143 //here we send all data to user event handlers
144 return _timerTickEventHandler(this, e);
150 /// Creates a tick timer that emits periodic signal.
152 /// <param name="milliSec">Interval in milliseconds.</param>
153 /// <returns>A new timer.</returns>
154 /// <since_tizen> 3 </since_tizen>
155 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
157 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 Tizen.Log.Error("NUI", $"({swigCPtr.Handle}) Timer({milliSec}) Constructor!");
161 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
163 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167 /// Downcasts a handle to Timer handle.
169 /// <since_tizen> 3 </since_tizen>
170 /// Please do not use! this will be deprecated!
171 /// Instead please use as keyword.
172 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
173 [EditorBrowsable(EditorBrowsableState.Never)]
174 public static Timer DownCast(BaseHandle handle)
176 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
177 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182 /// Starts the timer.<br />
183 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
185 /// <since_tizen> 3 </since_tizen>
189 NDalicPINVOKE.Timer_Start(swigCPtr);
191 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
197 /// <since_tizen> 3 </since_tizen>
201 NDalicPINVOKE.Timer_Stop(swigCPtr);
203 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207 /// Gets/Sets the interval of the timer.
209 /// <remarks>For setter, this sets a new interval on the timer and starts the timer. <br />
210 /// Cancels the previous timer.
212 /// <since_tizen> 4 </since_tizen>
217 return GetInterval();
226 /// Sets a new interval on the timer and starts the timer.<br />
227 /// Cancels the previous timer.<br />
229 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
230 internal void SetInterval(uint milliSec)
233 Tizen.Log.Error("NUI", $"({swigCPtr.Handle}) SetInterval({milliSec})");
236 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
237 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
240 internal uint GetInterval()
242 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
243 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
244 Tizen.Log.Error("NUI", $"({swigCPtr.Handle})GetInterval({ret})");
249 /// Tells whether the timer is running.
251 /// <returns>Whether the timer is started or not.</returns>
252 /// <since_tizen> 3 </since_tizen>
253 public bool IsRunning()
255 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
256 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
260 internal TimerSignalType TickSignal()
262 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
263 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();