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)
70 NDalicPINVOKE.delete_Timer(swigCPtr);
72 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
80 /// Event arguments that passed via Tick event.
82 public class TickEventArgs : EventArgs
86 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
87 private delegate bool TickCallbackDelegate(IntPtr data);
88 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
89 private TickCallbackDelegate _timerTickCallbackDelegate;
92 /// brief Event for Ticked signal which can be used to subscribe/unsubscribe the event handler
93 /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>).<br>
94 /// provided by the user. Ticked signal is emitted after specified time interval.<br>
96 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
100 if (_timerTickEventHandler == null)
102 _timerTickCallbackDelegate = (OnTick);
103 TickSignal().Connect(_timerTickCallbackDelegate);
105 _timerTickEventHandler += value;
109 _timerTickEventHandler -= value;
110 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
112 TickSignal().Disconnect(_timerTickCallbackDelegate);
117 private bool OnTick(IntPtr data)
119 TickEventArgs e = new TickEventArgs();
121 if (_timerTickEventHandler != null)
123 //here we send all data to user event handlers
124 return _timerTickEventHandler(this, e);
130 /// Creates a tick Timer that emits periodic signal.
132 /// <param name="milliSec">Interval in milliseconds</param>
133 /// <returns>A new timer</returns>
134 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
136 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
139 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
141 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
145 [Obsolete("Please do not use! this will be deprecated")]
146 public static Timer DownCast(BaseHandle handle)
148 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
149 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154 /// Starts timer.<br>
155 /// In case a Timer is already running, its time is reset and timer is restarted.<br>
159 NDalicPINVOKE.Timer_Start(swigCPtr);
160 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168 NDalicPINVOKE.Timer_Stop(swigCPtr);
169 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173 /// Sets a new interval on the timer and starts the timer.<br>
174 /// Cancels the previous timer.<br>
176 /// <param name="milliSec">milliSec Interval in milliseconds</param>
177 internal void SetInterval(uint milliSec)
179 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
180 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
183 internal uint GetInterval()
185 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
186 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191 /// Tells whether timer is running.
193 /// <returns>Whether Timer is started or not</returns>
194 public bool IsRunning()
196 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
197 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201 internal TimerSignalType TickSignal()
203 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
204 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();