1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
21 using System.Runtime.InteropServices;
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 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
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;
47 protected override void Dispose(DisposeTypes type)
54 if(type == DisposeTypes.Explicit)
57 //Release your own managed resources here.
58 //You should release all of your own disposable objects here.
61 //Release your own unmanaged resources here.
62 //You should not access any managed member here except static instance.
63 //because the execution order of Finalizes is non-deterministic.
65 if (swigCPtr.Handle != global::System.IntPtr.Zero)
71 //Unreference this instance from Registry.
72 Registry.Unregister(this);
74 NDalicPINVOKE.delete_Timer(swigCPtr);
76 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
84 /// Event arguments that passed via Tick event.
86 public class TickEventArgs : EventArgs
90 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
91 private delegate bool TickCallbackDelegate(IntPtr data);
92 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
93 private TickCallbackDelegate _timerTickCallbackDelegate;
96 /// brief Event for Ticked signal which can be used to subscribe/unsubscribe the event handler
97 /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>).<br>
98 /// provided by the user. Ticked signal is emitted after specified time interval.<br>
100 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
104 if (_timerTickEventHandler == null)
106 _timerTickCallbackDelegate = (OnTick);
107 TickSignal().Connect(_timerTickCallbackDelegate);
109 _timerTickEventHandler += value;
113 _timerTickEventHandler -= value;
114 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
116 TickSignal().Disconnect(_timerTickCallbackDelegate);
121 private bool OnTick(IntPtr data)
123 TickEventArgs e = new TickEventArgs();
125 if (_timerTickEventHandler != null)
127 //here we send all data to user event handlers
128 return _timerTickEventHandler(this, e);
134 /// Creates a tick Timer that emits periodic signal.
136 /// <param name="milliSec">Interval in milliseconds</param>
137 /// <returns>A new timer</returns>
138 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
140 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
143 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
145 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149 [Obsolete("Please do not use! this will be deprecated")]
150 public static Timer DownCast(BaseHandle handle)
152 Timer ret = new Timer(NDalicPINVOKE.Timer_DownCast(BaseHandle.getCPtr(handle)), true);
153 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158 /// Starts timer.<br>
159 /// In case a Timer is already running, its time is reset and timer is restarted.<br>
163 NDalicPINVOKE.Timer_Start(swigCPtr);
164 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
172 NDalicPINVOKE.Timer_Stop(swigCPtr);
173 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
177 /// Sets a new interval on the timer and starts the timer.<br>
178 /// Cancels the previous timer.<br>
180 /// <param name="milliSec">milliSec Interval in milliseconds</param>
181 internal void SetInterval(uint milliSec)
183 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
184 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187 internal uint GetInterval()
189 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
190 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195 /// Tells whether timer is running.
197 /// <returns>Whether Timer is started or not</returns>
198 public bool IsRunning()
200 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
201 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205 internal TimerSignalType TickSignal()
207 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
208 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();