如何立即退出程序? [英] How to exit the program immediately?

查看:36
本文介绍了如何立即退出程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VB 6 和 Access 2003

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:

从数据库中获取数据并创建记录集,创建临时表...,

有没有什么办法可以在VB代码使用数据库的时候立即退出程序(比如创建记录集,创建临时表)?

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?

需要 VB 6 代码帮助

Need VB 6 Code Help

推荐答案

它可能挂起的另一个原因是因为 VB6 正在等待数据库.您在数据库上运行的查询是长时间运行的,还是需要很长时间的 VB6 代码?

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?

如果数据库查询导致问题并且您使用的是 ADO,则可以使用异步数据库连接.代码的逻辑可能会更复杂,因为您必须设置回调方法以在每次数据库调用完成后继续处理.

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.

然而,据我所知,如果您不使用异步查询方法,VB6 将保持无响应,直到您的 m_conn.Execute 方法返回.

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.

如果延迟是由于 VB6 代码本身造成的——例如在你创建临时表的所有工作中,那么我会同意 MarkJ 的经典"答案并使用 DoEvents——尽管看起来是一个笨拙的机制,它确实工作得很好.在整个过程中撒上一些,您应该会注意到一些改善.虽然可能不完美,但可能已经足够了.

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.

例如

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

每次点击 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

这篇关于如何立即退出程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆