DynaPDF Manual - Page 35

Previous Page 34   Index   Next Page 36

Language Bindings
Page 35 of 839
break button). 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 functions inside of a cloned function. When DynaPDF tries to
raise an event inside the cloned function an access violation occurs and VB crashes.
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 checks whether a previous call of the function is running before the function
can be executed again; a quite simple but effective solution that makes your application stabile.
Example:
Option Explicit
Private WithEvents FPDF As CPDF ' Enable event support
Private Sub Form_Load()
' We hold one instance of the class CPDF in memory
Set FPDF = new CPDF
If FPDF Is Nothing Then
MsgBox "Out of memory!", vbCritical, "Fatal error"
End If
End Sub
Private Sub Form_Terminate()
' Delete the class instance
Set FPDF = Nothing
End Sub
Private Sub Command1_Click()
FPDF.CreateNewPDFA "c:/vbout.pdf"
FPDF.SetDocInfoA diAuthor, "Jens Boschulte"
FPDF.SetDocInfoA diSubject, "My first VB output"
FPDF.SetDocInfoA diTitle, "My first VB output"
FPDF.Append
FPDF.SetFont "Arial", fsItalic, 30#, True, cp1252
FPDF.WriteFTextA taCenter, "My first VB output"
FPDF.EndPage
 

Previous topic: The DoEvents problem

Next topic: Visual Basic .Net, 64 Bit Applications, General Note: