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;
38 private bool played = false;
39 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
40 private TickCallbackDelegate _timerTickCallbackDelegate;
42 private System.IntPtr _timerTickCallbackOfNative;
45 /// Creates a tick timer that emits periodic signal.
47 /// <param name="milliSec">Interval in milliseconds.</param>
48 /// <returns>A new timer.</returns>
49 /// <since_tizen> 3 </since_tizen>
50 public Timer(uint milliSec) : this(Interop.Timer.Timer_New(milliSec), true)
52 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer({milliSec}) Constructor!");
56 internal Timer(Timer timer) : this(Interop.Timer.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
58 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
61 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.Timer.Timer_SWIGUpcast(cPtr), cMemoryOwn)
63 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
65 _timerTickCallbackDelegate = OnTick;
66 _timerTickCallbackOfNative = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(_timerTickCallbackDelegate);
68 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() contructor!");
76 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() distructor!, disposed={disposed}");
79 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
80 private delegate bool TickCallbackDelegate();
83 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
84 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
86 /// <since_tizen> 3 </since_tizen>
87 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
91 if (_timerTickEventHandler == null && disposed == false)
93 TickSignal().Connect(_timerTickCallbackOfNative);
95 _timerTickEventHandler += value;
99 _timerTickEventHandler -= value;
100 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
102 TickSignal().Disconnect(_timerTickCallbackOfNative);
108 /// Gets/Sets the interval of the timer.
110 /// <remarks>For setter, this sets a new interval on the timer and starts the timer. <br />
111 /// Cancels the previous timer.
113 /// <since_tizen> 4 </since_tizen>
118 return GetInterval();
127 /// Starts the timer.<br />
128 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
130 /// <since_tizen> 3 </since_tizen>
134 Interop.Timer.Timer_Start(swigCPtr);
136 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142 /// <since_tizen> 3 </since_tizen>
146 Interop.Timer.Timer_Stop(swigCPtr);
148 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
152 /// Tells whether the timer is running.
154 /// <returns>Whether the timer is started or not.</returns>
155 /// <since_tizen> 3 </since_tizen>
156 public bool IsRunning()
158 bool ret = Interop.Timer.Timer_IsRunning(swigCPtr);
159 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
165 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
169 /// Sets a new interval on the timer and starts the timer.<br />
170 /// Cancels the previous timer.<br />
172 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
173 internal void SetInterval(uint milliSec)
175 NUILog.Debug($"(0x{swigCPtr.Handle:X})SetInterval({milliSec})");
179 Interop.Timer.Timer_SetInterval(swigCPtr, milliSec);
180 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
183 internal uint GetInterval()
185 uint ret = Interop.Timer.Timer_GetInterval(swigCPtr);
186 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190 internal TimerSignalType TickSignal()
192 TimerSignalType ret = new TimerSignalType(Interop.Timer.Timer_TickSignal(swigCPtr), false);
193 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200 /// <since_tizen> 3 </since_tizen>
201 protected override void Dispose(DisposeTypes type)
203 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer.Dispose(type={type}, disposed={disposed})");
205 if (this != null && _timerTickCallbackDelegate != null)
207 TickSignal().Disconnect(_timerTickCallbackOfNative);
215 if (type == DisposeTypes.Explicit)
218 //Release your own managed resources here.
219 //You should release all of your own disposable objects here.
222 //Release your own unmanaged resources here.
223 //You should not access any managed member here except static instance.
224 //because the execution order of Finalizes is non-deterministic.
226 if (swigCPtr.Handle != global::System.IntPtr.Zero)
231 Interop.Timer.delete_Timer(swigCPtr);
233 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
240 private bool OnTick()
242 TickEventArgs e = new TickEventArgs();
246 Tizen.Log.Fatal("NUI", $"(0x{swigCPtr.Handle:X}) OnTick() is called even played is false!");
247 //throw new System.InvalidOperationException($"OnTick() excpetion!");
250 if (_timerTickEventHandler != null && played == true)
252 //here we send all data to user event handlers
253 return _timerTickEventHandler(this, e);
259 /// Event arguments that passed via the tick event.
261 /// <since_tizen> 3 </since_tizen>
262 public class TickEventArgs : EventArgs