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;
40 private string stackTrace;
42 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
44 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
47 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
49 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
55 /// <since_tizen> 3 </since_tizen>
56 protected override void Dispose(DisposeTypes type)
63 if(type == DisposeTypes.Explicit)
66 //Release your own managed resources here.
67 //You should release all of your own disposable objects here.
70 //Release your own unmanaged resources here.
71 //You should not access any managed member here except static instance.
72 //because the execution order of Finalizes is non-deterministic.
74 if (_timerTickCallbackDelegate != null)
76 TickSignal().Disconnect(_timerTickCallbackDelegate);
79 if (swigCPtr.Handle != global::System.IntPtr.Zero)
84 NDalicPINVOKE.delete_Timer(swigCPtr);
86 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
95 /// Event arguments that passed via the tick event.
97 /// <since_tizen> 3 </since_tizen>
98 public class TickEventArgs : EventArgs
102 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
103 private delegate bool TickCallbackDelegate(IntPtr data);
104 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
105 private TickCallbackDelegate _timerTickCallbackDelegate;
108 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
109 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
111 /// <since_tizen> 3 </since_tizen>
112 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
116 if (_timerTickEventHandler == null)
118 _timerTickCallbackDelegate = new TickCallbackDelegate(OnTick);
119 TickSignal().Connect(_timerTickCallbackDelegate);
121 _timerTickEventHandler += value;
125 _timerTickEventHandler -= value;
126 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
128 TickSignal().Disconnect(_timerTickCallbackDelegate);
133 private bool OnTick(IntPtr data)
135 TickEventArgs e = new TickEventArgs();
139 Tizen.Log.Fatal("NUI", $"should not be here! OnTick()!swigCPtr={swigCPtr.Handle}");
140 Tizen.Log.Fatal("NUI", "should not be here! #################### TID = " + global::System.Threading.Thread.CurrentThread.ManagedThreadId);
141 Tizen.Log.Fatal("NUI", stackTrace);
144 if (_timerTickEventHandler != null && played == true)
146 //here we send all data to user event handlers
147 return _timerTickEventHandler(this, e);
153 /// Creates a tick timer that emits periodic signal.
155 /// <param name="milliSec">Interval in milliseconds.</param>
156 /// <returns>A new timer.</returns>
157 /// <since_tizen> 3 </since_tizen>
158 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
160 stackTrace = Environment.StackTrace;
161 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
166 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170 /// Downcasts a handle to Timer handle.
172 /// <since_tizen> 3 </since_tizen>
173 /// Please do not use! this will be deprecated!
174 /// Instead please use as keyword.
175 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
176 [EditorBrowsable(EditorBrowsableState.Never)]
177 public static Timer DownCast(BaseHandle handle)
179 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
180 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185 /// Starts the timer.<br />
186 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
188 /// <since_tizen> 3 </since_tizen>
192 NDalicPINVOKE.Timer_Start(swigCPtr);
194 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200 /// <since_tizen> 3 </since_tizen>
204 NDalicPINVOKE.Timer_Stop(swigCPtr);
206 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
210 /// Gets/Sets the interval of the 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)
232 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
233 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236 internal uint GetInterval()
238 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
239 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
244 /// Tells whether the timer is running.
246 /// <returns>Whether the timer is started or not.</returns>
247 /// <since_tizen> 3 </since_tizen>
248 public bool IsRunning()
250 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
251 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
255 internal TimerSignalType TickSignal()
257 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
258 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();