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;
21 using System.Diagnostics;
26 /// Mechanism to issue simple periodic or one-shot events.<br />
27 /// Timer is provided for application developers to be able to issue
28 /// simple periodic or one-shot events. Please note that the timer
29 /// callback functions should return as soon as possible because they
30 /// block the next SignalTick. Please note that timer signals are not
31 /// in sync with DALi's render timer.<br />
32 /// This class is a handle class so it can be stack allocated and used
33 /// as a member.<br />
35 /// <since_tizen> 3 </since_tizen>
36 public class Timer : BaseHandle
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)
64 _timerTickCallbackDelegate = OnTick;
65 _timerTickCallbackOfNative = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(_timerTickCallbackDelegate);
67 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() contructor!");
75 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() distructor!, disposed={disposed}");
78 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
79 private delegate bool TickCallbackDelegate();
82 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
83 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
85 /// <since_tizen> 3 </since_tizen>
86 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
90 if (_timerTickEventHandler == null && disposed == false)
92 TickSignal().Connect(_timerTickCallbackOfNative);
94 _timerTickEventHandler += value;
98 _timerTickEventHandler -= value;
99 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
101 TickSignal().Disconnect(_timerTickCallbackOfNative);
107 /// Gets/Sets the interval of the timer.
109 /// <remarks>For setter, this sets a new interval on the timer and starts the timer. <br />
110 /// Cancels the previous timer.
112 /// <since_tizen> 4 </since_tizen>
117 return GetInterval();
126 /// Starts the timer.<br />
127 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
129 /// <since_tizen> 3 </since_tizen>
132 if (Thread.CurrentThread.ManagedThreadId != 1)
134 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
136 StackTrace st = new StackTrace(true);
137 for (int i = 0; i < st.FrameCount; i++)
139 StackFrame sf = st.GetFrame(i);
140 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
144 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
146 NUILog.Error("[ERR] already disposed! can not get this done! just return here! please make sure that the handle gets free when using explicit Dispose()! For example, timer.Dispose(); timer = null; this must be done!");
151 Interop.Timer.Timer_Start(swigCPtr);
153 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 /// <since_tizen> 3 </since_tizen>
162 if (Thread.CurrentThread.ManagedThreadId != 1)
164 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
167 StackTrace st = new StackTrace(true);
168 for (int i = 0; i < st.FrameCount; i++)
170 StackFrame sf = st.GetFrame(i);
171 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
175 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
177 NUILog.Error("[ERR] already disposed! can not get this done! just return here! please make sure that the handle gets free when using explicit Dispose()! For example, timer.Dispose(); timer = null; this must be done!");
182 Interop.Timer.Timer_Stop(swigCPtr);
184 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188 /// Tells whether the timer is running.
190 /// <returns>Whether the timer is started or not.</returns>
191 /// <since_tizen> 3 </since_tizen>
192 public bool IsRunning()
194 if (Thread.CurrentThread.ManagedThreadId != 1)
196 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
198 StackTrace st = new StackTrace(true);
199 for (int i = 0; i < st.FrameCount; i++)
201 StackFrame sf = st.GetFrame(i);
202 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
206 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
208 NUILog.Error("[ERR] already disposed! can not get this done! just return here! please make sure that the handle gets free when using explicit Dispose()! For example, timer.Dispose(); timer = null; this must be done!");
212 bool ret = Interop.Timer.Timer_IsRunning(swigCPtr);
213 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
219 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
223 /// Sets a new interval on the timer and starts the timer.<br />
224 /// Cancels the previous timer.<br />
226 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
227 internal void SetInterval(uint milliSec)
229 NUILog.Debug($"(0x{swigCPtr.Handle:X})SetInterval({milliSec})");
231 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
233 NUILog.Error("[ERR] already disposed! can not get this done! just return here! please make sure that the handle gets free when using explicit Dispose()! For example, timer.Dispose(); timer = null; this must be done!");
239 Interop.Timer.Timer_SetInterval(swigCPtr, milliSec);
240 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
243 internal uint GetInterval()
245 if(swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
247 NUILog.Error("[ERR] already disposed! can not get this done! just return here! please make sure that the handle gets free when using explicit Dispose()! For example, timer.Dispose(); timer = null; this must be done!");
251 uint ret = Interop.Timer.Timer_GetInterval(swigCPtr);
252 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256 internal TimerSignalType TickSignal()
258 TimerSignalType ret = new TimerSignalType(Interop.Timer.Timer_TickSignal(swigCPtr), false);
259 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
266 /// <since_tizen> 3 </since_tizen>
267 protected override void Dispose(DisposeTypes type)
269 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer.Dispose(type={type}, disposed={disposed})");
271 if (this != null && _timerTickCallbackDelegate != null)
273 TickSignal().Disconnect(_timerTickCallbackOfNative);
285 /// This will not be public opened.
286 [EditorBrowsable(EditorBrowsableState.Never)]
287 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
289 Interop.Timer.delete_Timer(swigCPtr);
292 private bool OnTick()
294 TickEventArgs e = new TickEventArgs();
298 Tizen.Log.Fatal("NUI", $"(0x{swigCPtr.Handle:X}) OnTick() is called even played is false!");
299 //throw new System.InvalidOperationException($"OnTick() excpetion!");
302 if (_timerTickEventHandler != null && played == true)
304 //here we send all data to user event handlers
305 return _timerTickEventHandler(this, e);
311 /// Event arguments that passed via the tick event.
313 /// <since_tizen> 3 </since_tizen>
314 public class TickEventArgs : EventArgs