Function is still running after form.close().

S

Shelby

Hi,
I still fail to properly end a function call with the below codes.
How can I terminate all function calls?

--------------------------------------------------------------
Connect.vb
--------------------------------------------------------------
Dim WithEvents frm1 As New ProcessForm
Private Sub Button1_Click(ByVal Ctrl As CommandBarButton, ByRef
CancelDefault As Boolean) Handles Button1.Click
frm1 = New ProcessForm
frm1.oNS = Me.oNS
frm1.oApp = Me.oApp
frm1 .Show()
End Sub

Private Sub frm1_Closed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles frm1.Closed
frm1 = Nothing
End Sub


--------------------------------------------------------------
ProcessForm.vb
--------------------------------------------------------------
' This form has a Start and Cancel button.
' Once user click Start, it will call a function to
' perform database operation which is about 10 minutes.
' I have place a System.Windows.Forms.Application.DoEvents() in the loop of
the function,
' so that user can click Cancel and stop the process.
' The problem is when user click Cancel, the form and the database
connection is closed.
' But it is still looping in the function, because it raise an error that
there's no database connection.
' How can I resolve this problem?

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncancel.Click
DBconn.Close()
Me.Close()
End Sub

Thanks
Shelby
 
C

Cor Ligthert

Hi Shelby,

Your error is raised in the database part.

However we have no clue how you do that, I assume that it is a datareader,
because than you can probably iterrupt it, however you never told us that,
while that is the part that has to be stopped and there should than the
action take place in my opinion.

So show us that part of the code, maybe we can than help you?

Cor
 
S

Shelby

Hi Cor,
ok.. Sorry I left that out... I hope the below helps.


---------------------------
Connect.vb
---------------------------
' this is the base class for COM Addin for Outlook

Dim WithEvents frm1 As New ProcessForm
Dim WithEvents Sub Button1 As CommandBarButton

Private Sub Button1_Click(ByVal Ctrl As CommandBarButton, ByRef
CancelDefault As Boolean) Handles Button1.Click
frm1 = New ProcessForm
frm1 .Show()
End Sub

Private Sub frm1_Closed(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles frm1.Closed
frm1 = Nothing
End Sub



----------------------------
ProcessForm.vb
----------------------------
' this form is basically GUI. Make certains selection and click Start to
call ProcessEngine Object.

Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnstart.Click
Dim cn as ADODB.Connection
cn = OpenConnection() ' this will open database connection

Dim objPE as New ProcessEngine
objPE.cn = cn
objPE.GetDBRecords()
objPE = Nothing
End Sub

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncancel.Click
cn.Close()
Me.Close()
End Sub

----------------------------
ProcessEngine.vb
----------------------------
Public cn as ADODB.Connection
Dim MyCus as hashtable

Public Sub New()
MyCus = New Hashtable
End Sub

Public Sub GetDBRecords()
' Open recordset and store to array first.
Dim tmparr = rs.GetRows()



for i as integer =0 to ubound(tmparr,2)-1
' the actual problem starts here.
' if user click Cancel when it is in this loop,
' it will continue to run this process.
' Note that cn is closed when user click Cancel

System.Windows.Forms.Application.DoEvents()

rs2.open("select AttID from customeratt where CusID=" & tmparr(0,i),
cn)
while not rs2.eof
' do some processing here
rs2.movenext
end while
next
End Sub



Thanks
Shelby
 
C

Cor Ligthert

Hi Shelby,

Just an idea
You are using classic ADO and probably you will change that in future, so
maybe you can do it quick and dirty, when you want to do it clean reply
than.

The error is probably reported in that ADO loop rs2.
When you set a shared boolswitch when you are closing and set set the Ado
loop in a try block
than you can test in the catch block if the closewitch is true. Than you can
in my idea close the rs2 and than maybe (I think probably) your problems are
gone.

We are not so much ADO related here anymore.

However this is a quick and dirty solution which I would try first.

I hope this helps.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top