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 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
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 (_timerTickCallbackDelegate != null)
67 TickSignal().Disconnect(_timerTickCallbackDelegate);
70 if (swigCPtr.Handle != global::System.IntPtr.Zero)
75 NDalicPINVOKE.delete_Timer(swigCPtr);
77 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
85 /// Event arguments that passed via the tick event.
87 public class TickEventArgs : EventArgs
91 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
92 private delegate bool TickCallbackDelegate(IntPtr data);
93 private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
94 private TickCallbackDelegate _timerTickCallbackDelegate;
97 /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
98 /// (in the type of TickEventHandler-DaliEventHandlerWithReturnType<object,TickEventArgs,bool>)<br>
99 /// provided by the user. The ticked signal is emitted after specified time interval.<br>
101 public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
105 if (_timerTickEventHandler == null)
107 _timerTickCallbackDelegate = (OnTick);
108 TickSignal().Connect(_timerTickCallbackDelegate);
110 _timerTickEventHandler += value;
114 _timerTickEventHandler -= value;
115 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
117 TickSignal().Disconnect(_timerTickCallbackDelegate);
122 private bool OnTick(IntPtr data)
124 TickEventArgs e = new TickEventArgs();
126 if (_timerTickEventHandler != null)
128 //here we send all data to user event handlers
129 return _timerTickEventHandler(this, e);
135 /// Creates a tick timer that emits periodic signal.
137 /// <param name="milliSec">Interval in milliseconds.</param>
138 /// <returns>A new timer.</returns>
139 public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
141 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
144 internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
146 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
150 [Obsolete("Please do not use! this will be deprecated")]
151 public static Timer DownCast(BaseHandle handle)
153 Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
154 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 /// Starts the timer.<br>
160 /// In case a timer is already running, its time is reset and the timer is restarted.<br>
164 NDalicPINVOKE.Timer_Start(swigCPtr);
165 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173 NDalicPINVOKE.Timer_Stop(swigCPtr);
174 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178 /// Gets/Sets the interval of the timer.
184 return GetInterval();
193 /// Sets a new interval on the timer and starts the timer.<br>
194 /// Cancels the previous timer.<br>
196 /// <param name="milliSec">MilliSec interval in milliseconds.</param>
197 internal void SetInterval(uint milliSec)
199 NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
203 internal uint GetInterval()
205 uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
206 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
211 /// Tells whether the timer is running.
213 /// <returns>Whether the timer is started or not.</returns>
214 public bool IsRunning()
216 bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
217 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
221 internal TimerSignalType TickSignal()
223 TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
224 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();