DynaPDF Manual - Page 20

Previous Page 19   Index   Next Page 21

Exception handling
Page 20 of 839
Private Sub FPDF_Error(ByVal Description As String, ByVal ErrType As
Long, DoBreak As Boolean)
' Add your code here
End Sub
Now you can enter some code that should be executed when the event is fired. Note that you
must still create an instance of the class CPDF before a DynaPDF function can be executed (see
Language bindings/Visual Basic for further information).
The handling of events in VB .Net is exactly the same as in VB 6.0.
Special issues in Visual Basic and .Net
The usage of events in Visual Basic or VB .Net is quite easy; however, there is a special behaviour
that must be taken into account when developing VB applications. When using the DoEvents
procedure in a VB function you must make sure that the function cannot be executed again while
a previous call of the function is still running.
DoEvents enables the asynchronous processing of the message loop so that the user interface can
be updated and the user can execute something while a function is running (e.g. break
processing). DoEvents is often used because it is an easy way to avoid blocking of an application
without using of threads.
However, when using DoEvents it is possible that a user clicks on the button again that executes
DynaPDF functions while a previous call is still running. This is normally no problem but it is
impossible to execute an event function inside of a cloned function. When DynaPDF tries to raise
an event inside the cloned function an access violation occurs and VB crashes. VB .Net does not
crash but raises a System.NullReference exception in that case.
To avoid such problems check whether the function is still running:
Option Explicit
Private WithEvents FPDF As CPDF 'Enable event support
Private FRunning As Boolean
Private Sub Command1_Click()
If FRunning Then Exit Sub 'Check whether a previous call is running
FRunning = True
'Call some DynaPDF functions here...
DoEvents 'Process messages
FRunning = False
End Sub
The code above simply checks whether a previous call of the function is running before the
function can be executed again.

Previous topic: Exception handling in Visual Basic, Visual Basic .Net

Next topic: Customized Exception handling