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 (Thread.CurrentThread.ManagedThreadId != 1)
54 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
56 StackTrace st = new StackTrace(true);
57 for (int i = 0; i < st.FrameCount; i++)
59 StackFrame sf = st.GetFrame(i);
60 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
64 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
66 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer({milliSec}) Constructor!");
68 internal Timer(Timer timer) : this(Interop.Timer.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
70 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
73 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.Timer.Timer_SWIGUpcast(cPtr), cMemoryOwn)
76 _timerTickCallbackDelegate = OnTick;
77 _timerTickCallbackOfNative = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(_timerTickCallbackDelegate);
79 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() contructor!");
87 NUILog.Debug($"(0x{swigCPtr.Handle:X})Timer() distructor!, disposed={disposed}");
90 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
91 private delegate bool TickCallbackDelegate();
94 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
95 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
97 /// <since_tizen> 3 </since_tizen>
98 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
102 if (Thread.CurrentThread.ManagedThreadId != 1)
104 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
106 StackTrace st = new StackTrace(true);
107 for (int i = 0; i < st.FrameCount; i++)
109 StackFrame sf = st.GetFrame(i);
110 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
115 if (_timerTickEventHandler == null && disposed == false)
117 TickSignal().Connect(_timerTickCallbackOfNative);
119 _timerTickEventHandler += value;
123 if (Thread.CurrentThread.ManagedThreadId != 1)
125 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
127 StackTrace st = new StackTrace(true);
128 for (int i = 0; i < st.FrameCount; i++)
130 StackFrame sf = st.GetFrame(i);
131 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
135 _timerTickEventHandler -= value;
136 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
138 TickSignal().Disconnect(_timerTickCallbackOfNative);
144 /// Gets/Sets the interval of the timer.
146 /// <remarks>For setter, this sets a new interval on the timer and starts the timer. <br />
147 /// Cancels the previous timer.
149 /// <since_tizen> 4 </since_tizen>
154 if (Thread.CurrentThread.ManagedThreadId != 1)
156 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
158 StackTrace st = new StackTrace(true);
159 for (int i = 0; i < st.FrameCount; i++)
161 StackFrame sf = st.GetFrame(i);
162 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
166 return GetInterval();
170 if (Thread.CurrentThread.ManagedThreadId != 1)
172 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
174 StackTrace st = new StackTrace(true);
175 for (int i = 0; i < st.FrameCount; i++)
177 StackFrame sf = st.GetFrame(i);
178 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
187 /// Starts the timer.<br />
188 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
190 /// <since_tizen> 3 </since_tizen>
193 if (Thread.CurrentThread.ManagedThreadId != 1)
195 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
197 StackTrace st = new StackTrace(true);
198 for (int i = 0; i < st.FrameCount; i++)
200 StackFrame sf = st.GetFrame(i);
201 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
205 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
207 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 Interop.Timer.Timer_Start(swigCPtr);
214 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220 /// <since_tizen> 3 </since_tizen>
223 if (Thread.CurrentThread.ManagedThreadId != 1)
225 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
228 StackTrace st = new StackTrace(true);
229 for (int i = 0; i < st.FrameCount; i++)
231 StackFrame sf = st.GetFrame(i);
232 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
236 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
238 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!");
243 Interop.Timer.Timer_Stop(swigCPtr);
245 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
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 if (Thread.CurrentThread.ManagedThreadId != 1)
257 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
259 StackTrace st = new StackTrace(true);
260 for (int i = 0; i < st.FrameCount; i++)
262 StackFrame sf = st.GetFrame(i);
263 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
267 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
269 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!");
273 bool ret = Interop.Timer.Timer_IsRunning(swigCPtr);
274 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
278 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
280 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
284 /// Sets a new interval on the timer and starts the timer.<br />
285 /// Cancels the previous timer.<br />
287 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
288 internal void SetInterval(uint milliSec)
290 NUILog.Debug($"(0x{swigCPtr.Handle:X})SetInterval({milliSec})");
292 if (swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
294 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!");
300 Interop.Timer.Timer_SetInterval(swigCPtr, milliSec);
301 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
304 internal uint GetInterval()
306 if(swigCPtr.Handle == global::System.IntPtr.Zero || disposed)
308 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!");
312 uint ret = Interop.Timer.Timer_GetInterval(swigCPtr);
313 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317 internal TimerSignalType TickSignal()
319 TimerSignalType ret = new TimerSignalType(Interop.Timer.Timer_TickSignal(swigCPtr), false);
320 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327 /// <since_tizen> 3 </since_tizen>
328 protected override void Dispose(DisposeTypes type)
330 if (Thread.CurrentThread.ManagedThreadId != 1)
332 Tizen.Log.Error("NUI", "current threadID : " + Thread.CurrentThread.ManagedThreadId);
334 StackTrace st = new StackTrace(true);
335 for (int i = 0; i < st.FrameCount; i++)
337 StackFrame sf = st.GetFrame(i);
338 Tizen.Log.Error("NUI", " Method " + sf.GetMethod());
342 NUILog.Debug($"(0x{swigCPtr.Handle:X}) Timer.Dispose(type={type}, disposed={disposed})");
344 if (this != null && _timerTickCallbackDelegate != null)
346 TickSignal().Disconnect(_timerTickCallbackOfNative);
358 /// This will not be public opened.
359 [EditorBrowsable(EditorBrowsableState.Never)]
360 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
362 Interop.Timer.delete_Timer(swigCPtr);
365 private bool OnTick()
367 TickEventArgs e = new TickEventArgs();
371 Tizen.Log.Fatal("NUI", $"(0x{swigCPtr.Handle:X}) OnTick() is called even played is false!");
372 //throw new System.InvalidOperationException($"OnTick() excpetion!");
375 if (_timerTickEventHandler != null && played == true)
377 //here we send all data to user event handlers
378 return _timerTickEventHandler(this, e);
384 /// Event arguments that passed via the tick event.
386 /// <since_tizen> 3 </since_tizen>
387 public class TickEventArgs : EventArgs