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 /// <since_tizen> 3 </since_tizen>
34 public class Timer : BaseHandle
36 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
38 internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
40 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
43 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
45 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
51 /// <since_tizen> 3 </since_tizen>
52 protected override void Dispose(DisposeTypes type)
59 if(type == DisposeTypes.Explicit)
62 //Release your own managed resources here.
63 //You should release all of your own disposable objects here.
66 //Release your own unmanaged resources here.
67 //You should not access any managed member here except static instance.
68 //because the execution order of Finalizes is non-deterministic.
70 if (_timerTickCallbackDelegate != null)
72 TickSignal().Disconnect(_timerTickCallbackDelegate);
75 if (swigCPtr.Handle != global::System.IntPtr.Zero)
80 NDalicPINVOKE.delete_Timer(swigCPtr);
82 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
90 /// Event arguments that passed via the tick event.
92 /// <since_tizen> 3 </since_tizen>
93 public class TickEventArgs : EventArgs
97 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
98 private delegate bool TickCallbackDelegate(IntPtr data);
99 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
100 private TickCallbackDelegate _timerTickCallbackDelegate;
103 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
104 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
106 /// <since_tizen> 3 </since_tizen>
107 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
111 if (_timerTickEventHandler == null)
113 _timerTickCallbackDelegate = (OnTick);
114 TickSignal().Connect(_timerTickCallbackDelegate);
116 _timerTickEventHandler += value;
120 _timerTickEventHandler -= value;
121 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
123 TickSignal().Disconnect(_timerTickCallbackDelegate);
128 private bool OnTick(IntPtr data)
130 TickEventArgs e = new TickEventArgs();
132 if (_timerTickEventHandler != null)
134 //here we send all data to user event handlers
135 return _timerTickEventHandler(this, e);
141 /// Creates a tick timer that emits periodic signal.
143 /// <param name="milliSec">Interval in milliseconds.</param>
144 /// <returns>A new timer.</returns>
145 /// <since_tizen> 3 </since_tizen>
146 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
148 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
151 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
153 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
157 /// Downcasts a handle to Timer handle.
159 /// <since_tizen> 3 </since_tizen>
160 /// Please do not use! this will be deprecated!
161 /// Instead please use as keyword.
162 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public static Timer DownCast(BaseHandle handle)
166 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
167 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
172 /// Starts the timer.<br />
173 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
175 /// <since_tizen> 3 </since_tizen>
178 NDalicPINVOKE.Timer_Start(swigCPtr);
179 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185 /// <since_tizen> 3 </since_tizen>
188 NDalicPINVOKE.Timer_Stop(swigCPtr);
189 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
193 /// Gets/Sets the interval of the timer.
195 /// <since_tizen> 4 </since_tizen>
200 return GetInterval();
209 /// Sets a new interval on the timer and starts the timer.<br />
210 /// Cancels the previous timer.<br />
212 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
213 internal void SetInterval(uint milliSec)
215 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
216 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
219 internal uint GetInterval()
221 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
222 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227 /// Tells whether the timer is running.
229 /// <returns>Whether the timer is started or not.</returns>
230 /// <since_tizen> 3 </since_tizen>
231 public bool IsRunning()
233 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
234 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
238 internal TimerSignalType TickSignal()
240 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
241 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();