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);
45 _timerTickCallbackDelegate = OnTick;
46 _timerTickCallbackOfNative = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(_timerTickCallbackDelegate);
48 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() contructor!");
53 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() distructor!, disposed={disposed}");
56 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
58 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
64 /// <since_tizen> 3 </since_tizen>
65 protected override void Dispose(DisposeTypes type)
67 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer.Dispose(type={type}, disposed={disposed})");
69 if (this != null && _timerTickCallbackDelegate != null)
71 TickSignal().Disconnect(_timerTickCallbackOfNative);
79 if (type == DisposeTypes.Explicit)
82 //Release your own managed resources here.
83 //You should release all of your own disposable objects here.
86 //Release your own unmanaged resources here.
87 //You should not access any managed member here except static instance.
88 //because the execution order of Finalizes is non-deterministic.
90 if (swigCPtr.Handle != global::System.IntPtr.Zero)
95 NDalicPINVOKE.delete_Timer(swigCPtr);
97 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
106 /// Event arguments that passed via the tick event.
108 /// <since_tizen> 3 </since_tizen>
109 public class TickEventArgs : EventArgs
113 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
114 private delegate bool TickCallbackDelegate();
115 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
116 private TickCallbackDelegate _timerTickCallbackDelegate;
118 private System.IntPtr _timerTickCallbackOfNative;
121 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
122 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
124 /// <since_tizen> 3 </since_tizen>
125 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
129 if (_timerTickEventHandler == null && disposed == false)
131 TickSignal().Connect(_timerTickCallbackOfNative);
133 _timerTickEventHandler += value;
137 _timerTickEventHandler -= value;
138 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
140 TickSignal().Disconnect(_timerTickCallbackOfNative);
145 private bool OnTick()
147 TickEventArgs e = new TickEventArgs();
151 Tizen.Log.Fatal("NUI", $"(0x{swigCPtr.Handle:X}) OnTick() is called even played is false!");
152 //throw new System.InvalidOperationException($"OnTick() excpetion!");
155 if (_timerTickEventHandler != null && played == true)
157 //here we send all data to user event handlers
158 return _timerTickEventHandler(this, e);
164 /// Creates a tick timer that emits periodic signal.
166 /// <param name="milliSec">Interval in milliseconds.</param>
167 /// <returns>A new timer.</returns>
168 /// <since_tizen> 3 </since_tizen>
169 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
171 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer({milliSec}) Constructor!");
175 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
177 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181 /// Starts the timer.<br />
182 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
184 /// <since_tizen> 3 </since_tizen>
188 NDalicPINVOKE.Timer_Start(swigCPtr);
190 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
196 /// <since_tizen> 3 </since_tizen>
200 NDalicPINVOKE.Timer_Stop(swigCPtr);
202 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206 /// Gets/Sets the interval of the timer.
208 /// <remarks>For setter, this sets a new interval on the timer and starts the timer. <br />
209 /// Cancels the previous timer.
211 /// <since_tizen> 4 </since_tizen>
216 return GetInterval();
225 /// Sets a new interval on the timer and starts the timer.<br />
226 /// Cancels the previous timer.<br />
228 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
229 internal void SetInterval(uint milliSec)
231 NUILog.Debug($"(0x{swigCPtr.Handle:X})SetInterval({milliSec})");
235 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
236 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239 internal uint GetInterval()
241 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
242 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
247 /// Tells whether the timer is running.
249 /// <returns>Whether the timer is started or not.</returns>
250 /// <since_tizen> 3 </since_tizen>
251 public bool IsRunning()
253 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
254 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258 internal TimerSignalType TickSignal()
260 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
261 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();