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.
22 using System.Runtime.InteropServices;
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 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 public class TickEventArgs : EventArgs
96 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
97 private delegate bool TickCallbackDelegate(IntPtr data);
98 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
99 private TickCallbackDelegate _timerTickCallbackDelegate;
102 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
103 /// provided by the user. The ticked signal is emitted after specified time interval.<br />
105 /// <since_tizen> 3 </since_tizen>
106 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
110 if (_timerTickEventHandler == null)
112 _timerTickCallbackDelegate = (OnTick);
113 TickSignal().Connect(_timerTickCallbackDelegate);
115 _timerTickEventHandler += value;
119 _timerTickEventHandler -= value;
120 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
122 TickSignal().Disconnect(_timerTickCallbackDelegate);
127 private bool OnTick(IntPtr data)
129 TickEventArgs e = new TickEventArgs();
131 if (_timerTickEventHandler != null)
133 //here we send all data to user event handlers
134 return _timerTickEventHandler(this, e);
140 /// Creates a tick timer that emits periodic signal.
142 /// <param name="milliSec">Interval in milliseconds.</param>
143 /// <returns>A new timer.</returns>
144 /// <since_tizen> 3 </since_tizen>
145 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
147 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
152 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
156 /// [Obsolete("Please do not use! this will be deprecated")]
158 /// <since_tizen> 3 </since_tizen>
159 [Obsolete("Please do not use! this will be deprecated")]
160 public static Timer DownCast(BaseHandle handle)
162 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
163 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168 /// Starts the timer.<br />
169 /// In case a timer is already running, its time is reset and the timer is restarted.<br />
171 /// <since_tizen> 3 </since_tizen>
174 NDalicPINVOKE.Timer_Start(swigCPtr);
175 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
181 /// <since_tizen> 3 </since_tizen>
184 NDalicPINVOKE.Timer_Stop(swigCPtr);
185 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
189 /// Gets/Sets the interval of the timer.
191 /// <since_tizen> 4 </since_tizen>
196 return GetInterval();
205 /// Sets a new interval on the timer and starts the timer.<br />
206 /// Cancels the previous timer.<br />
208 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
209 internal void SetInterval(uint milliSec)
211 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
212 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
215 internal uint GetInterval()
217 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
218 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223 /// Tells whether the timer is running.
225 /// <returns>Whether the timer is started or not.</returns>
226 /// <since_tizen> 3 </since_tizen>
227 public bool IsRunning()
229 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
230 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234 internal TimerSignalType TickSignal()
236 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
237 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();