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;
24 /// Mechanism to issue simple periodic or one-shot events.<br />
25 /// Timer is provided for application developers to be able to issue
26 /// simple periodic or one-shot events. Please note that the timer
27 /// callback functions should return as soon as possible because they
28 /// block the next SignalTick. Please note that timer signals are not
29 /// in sync with DALi's render timer.<br />
30 /// This class is a handle class so it can be stack allocated and used
31 /// as a member.<br />
33 public class Timer : BaseHandle
35 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
37 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
39 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
42 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
44 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
50 /// <since_tizen> 3 </since_tizen>
51 protected override void Dispose(DisposeTypes type)
58 if(type == DisposeTypes.Explicit)
61 //Release your own managed resources here.
62 //You should release all of your own disposable objects here.
65 //Release your own unmanaged resources here.
66 //You should not access any managed member here except static instance.
67 //because the execution order of Finalizes is non-deterministic.
69 if (_timerTickCallbackDelegate != null)
71 TickSignal().Disconnect(_timerTickCallbackDelegate);
74 if (swigCPtr.Handle != global::System.IntPtr.Zero)
79 NDalicPINVOKE.delete_Timer(swigCPtr);
81 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
89 /// Event arguments that passed via the tick event.
91 public class TickEventArgs : EventArgs
95 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
96 private delegate bool TickCallbackDelegate(IntPtr data);
97 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
98 private TickCallbackDelegate _timerTickCallbackDelegate;
101 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
102 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
104 /// <since_tizen> 3 </since_tizen>
105 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
109 if (_timerTickEventHandler == null)
111 _timerTickCallbackDelegate = (OnTick);
112 TickSignal().Connect(_timerTickCallbackDelegate);
114 _timerTickEventHandler += value;
118 _timerTickEventHandler -= value;
119 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
121 TickSignal().Disconnect(_timerTickCallbackDelegate);
126 private bool OnTick(IntPtr data)
128 TickEventArgs e = new TickEventArgs();
130 if (_timerTickEventHandler != null)
132 //here we send all data to user event handlers
133 return _timerTickEventHandler(this, e);
139 /// Creates a tick timer that emits periodic signal.
141 /// <param name="milliSec">Interval in milliseconds.</param>
142 /// <returns>A new timer.</returns>
143 /// <since_tizen> 3 </since_tizen>
144 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
146 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
151 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
155 /// Downcasts a handle to Timer handle.
157 /// <since_tizen> 3 </since_tizen>
158 /// Please do not use! this will be deprecated!
159 /// Instead please use as keyword.
160 [Obsolete("Please DO NOT use! This will be deprecated, instead please USE as keyword.")]
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public static Timer DownCast(BaseHandle handle)
164 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
165 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170 /// Starts the timer.<br />
171 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
173 /// <since_tizen> 3 </since_tizen>
176 NDalicPINVOKE.Timer_Start(swigCPtr);
177 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
183 /// <since_tizen> 3 </since_tizen>
186 NDalicPINVOKE.Timer_Stop(swigCPtr);
187 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191 /// Gets/Sets the interval of the timer.
193 /// <since_tizen> 4 </since_tizen>
198 return GetInterval();
207 /// Sets a new interval on the timer and starts the timer.<br />
208 /// Cancels the previous timer.<br />
210 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
211 internal void SetInterval(uint milliSec)
213 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
214 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217 internal uint GetInterval()
219 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
220 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225 /// Tells whether the timer is running.
227 /// <returns>Whether the timer is started or not.</returns>
228 /// <since_tizen> 3 </since_tizen>
229 public bool IsRunning()
231 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
232 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236 internal TimerSignalType TickSignal()
238 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
239 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();