OPCWARE

Software components for automation users to assemble interconnections between industrial devices and applications, productively.


Data Layers
Industrial Software Solutions

Home

Products

Download

Order

About

Contact

 

 


Secondary (OLE callback) Interfaces and Pure Virtual Functions in Visual Basic

Events-based Timer declaration and Use

Normally, you will use the XtraTimer™ events-based timer class, CXTLite. In Visual Basic, its use is identical to how you would obtain events from any other object that provides events - use the WithEvents keyword:

Private WithEvents oXtraTimer As CXTLite

Sub oXtraTimer_Time1()
'Your Code Goes Here
End Sub

Interface-based OLE Callback Timer declaration and Use

If you need to use the XtraTimer™ interface-based timer object, you need to take a slightly different approach:- You still need to declare an instance of the CXTLite class, but omit the WithEvents keyword. You must also use Visual Basic's Implements keyword:

Implements ITimer
Private oXtraTimer As CXTLite

Private Sub ITimer_Timer1Tick()
'Your Code Goes Here
End Sub

If you declare the XtraTimer™ CXTLite object WithEvents and also Implement ITimer, Visual Basic will give you the secondary interface as requested, but you will also get Timer events and these may not be required so the system is wasting time in raising those unwanted and unused events. So if you are using ITimer, omit WithEvents.

When Should I use Secondary Interfaces?

The callback timers exposed by the secondary interface in XtraTimer™ (i.e. the ITimer procedures) are really only made available for use in rare, demanding applications. You simply don't need to use them normally, because events will do just fine in 99% of applications. Most programmers should not need to use the callback timers at all, apart from for evaluation purposes. Other than that, it's a judgement call.

Does it work?

Here's the background:- Callbacks are marginally faster than COM events, due to the use of vtable binding rather than IDispatch binding as used by events. So definite benefits accrue in some scenarios where speed and precision are of the essence. Typically, the time lag of an event behind it's corresponding callback function is around 0.05ms, but of course this depends on the operating system and the hardware. At 1ms intervals, a difference of 0.05ms is 5%, whereas this difference is insignificant at 50ms intervals (0.1%). If accumulated error is an issue then the callbacks via the secondary interface may prove to be the better option.

Furthermore, if you have some critial operations required to occur in quick sequence while there are lots of other operations going on, events being fired etc., the callbacks will tend to execute much faster because they are called directly and immediately, whereas raising events just places the events into a message-handling queue which may already contain other events, all waiting to be processed at some stage in the future.

The Notification callback timers that use the Implements keyword have one major difference from event-driven timers when used in the VB IDE. You will notice that if you run this test program, and click on one of the buttons to display a message box, the events driven timer will be blocked in the IDE, while the callback timer continues to fire, even within the IDE! This brings great benefits in certain scenarios, we're sure you'll agree.

A Word of Caution

So as explained, this method is the fastest but can appear problematic as it can occasionally overwhelm VB's IDE with event calls. For example, set BreakInDebugMode property to false, and look what happens if you set a breakpoint whilst you are debugging - the OLE callbacks continue to be executed even when in break mode (something which can't happen with the event interface). The solution is to set the BreakInDebugMode property to true so the IDE won't be overwhelmed with relentless callbacks when debugging. Of course it is probably best if you always set this property to true, unless you specifically want to continue receiving XtraTimer™ callbacks whilst paused on a breakpoint during a debugging session in the VB IDE.



Product Focus

 


Some Recent Testimonials

 

Click Here to visit the OPCWare Online Shop Now Click Here to visit the OPCWare Online Shop Now

Pricing | Buy Now

What is OPC?

Data Layers is a member of the OPC Foundation

Order | Download | Products

Home | About | Contact