How to exit the program immediately?

倖福魔咒の 提交于 2020-01-04 04:36:26

问题


Using VB 6 and Access 2003

I am using two command buttons (process, cancel)

When I run the Program, press cancel button – The form unloads immediately.
If I press the Process button and then Cancel button, the form is not unloading immediately.

In Process button, I wrote the code like this:

Getting the data from the database and creating a record set, creating a temporary table…,

Is there any way to exit the program immediately at the time of VB code working with database (like Creating Record set, creating temporary tables)?

In Cancel button, I wrote this code

Unload me

How to exit the Program immediately when it is working with database?

Need VB 6 Code Help


回答1:


Another reason it may be hanging is because VB6 is waiting for the database. Are the queries you're running on the database long-running, or is it the VB6 code that's taking a long time?

If the database queries cause the problem and you're using ADO, you can use an asynchronous database connection. The logic can be more complex to code then, because you have to set up callback methods to continue the processing after each database call has finished.

You can establish such a connection doing something like this:

Private WithEvents m_conn As ADODB.Connection
...
Set m_conn = New ADODB.Connection
Call m_conn.Open(connectionString, , , adAsyncConnect)

Execute a query / stored procedure like this - the main difference you'll notice here is that instead of hanging until the query completes, code execution continues as normal):

sql = "SELECT Col1 FROM etc. etc."
Call m_conn.Execute(sql, , adAsyncExecute)

Cancel a running query like this:

Call m_conn.cancel

And the all important callback for when the query completes:

Private Sub m_conn_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
    ... do more processing etc...
End Sub

You may find that the above is overkill for what you want; I only implemented that myself because the window was specifically designed to do long-running queries, and it was quite important that the window remain responsive, and allow queries to be cancelled partway through.

As far as I know, however, if you don't use the asynchronous query method, VB6 will remain unresponsive until your m_conn.Execute method returns.

If the delay is occurring because of the VB6 code itself - for example in all the work you're doing creating temporary tables, then I'd agree with MarkJ's "classic" answer and use DoEvents - though seemingly a clumsy mechanism, it does work pretty well. Sprinkle a few of those throughout your procedure and you should notice some improvement. Though perhaps not perfection, it may be good enough.

e.g.

retrieve results
...
DoEvents
...
build temporary tables
...
DoEvents

Each time one of the DoEvents blocks is hit, if the user clicked the Cancel button that code should fire.

Note: You may also want to be a little bit wary about unloading a form while code is still running, I'm not convinced everything will unload properly. In that case you might want to have something like:

Private m_cancel as Boolean

Private Sub cmdCancel_Click()
    m_cancel = True
End Sub

Private Sub cmdProcess_Click()
    ...
    retrieve results
    ...
    DoEvents
    If Not m_cancel Then
        ... build temporary tables etc...
    End if

    If m_cancel Then Unload Me
End Sub



回答2:


Try:

End



回答3:


The problem is probably that the Cancel button Click event isn't firing, because your single thread is busy doing the processing.

  • The classic VB6 trick is to use DoEvents - see more here.
  • Or you can run the processing in a background thread. Perhaps by using the .NET BackgroundWorker component through Interop like this MSDN article says. EDIT this doesn't work!!
  • EDIT: or by delegating to an ActiveX exe that calls back when the task is finished. Like this.



回答4:


Shortly, write more DoEvents in Process button's code.
Or use DoEvents's alternative code , which is much better and faster than DoEvents. http://nirsoft.net/vb/doevents.html



来源:https://stackoverflow.com/questions/1264513/how-to-exit-the-program-immediately

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!