MS Access 中的 VBA + 线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/739089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:43:19  来源:igfitidea点击:

VBA + Threads in MS Access

multithreadingvbams-accessaccess-vba

提问by Curtis Inderwiesche

How can I create a process running on a separate thread in MS Access VBA? I would like to create a process that will just sit and wait for a message.

如何在 MS Access VBA 中创建在单独线程上运行的进程?我想创建一个只坐等消息的进程。

回答by ViggoV

You might want to check out this workaround: http://www.excelhero.com/blog/2010/05/multi-threaded-vba.html

您可能想查看此解决方法:http: //www.excelhero.com/blog/2010/05/multi-threaded-vba.html

It is Excel, but it should be practically the same. It works by building VBScript "agents" and have them execute tasks.. Check the example, it is quite impressive

它是 Excel,但实际上应该是相同的。它通过构建 VBScript“代理”并让它们执行任务来工作。检查示例,它非常令人印象深刻

-Viggo

-维果

回答by Reed Copsey

There is no way to do this directly in VBA itself. Here is a MSDN forum discussiontalking about this in detail. Office never exposed any of the VBA extensions for multithreading.

没有办法直接在 VBA 中做到这一点。 这是一个 MSDN 论坛讨论,详细讨论了这一点。Office 从未公开任何用于多线程的 VBA 扩展。

However, you can do this by calling out to the Windows API, or creating your own COM object in VBA (written elsewhere) which performs the multithreaded calls for you. Just make sure to marshall everything back to the calling thread, somehow (probably polling against your COM object, or something similar).

但是,您可以通过调用 Windows API 或在 VBA 中创建您自己的 COM 对象(在别处编写)来为您执行多线程调用。只需确保以某种方式将所有内容编组回调用线程(可能轮询您的 COM 对象,或类似的东西)。

Also, you may want to check out bendewey's link on COM threading, since it's very relevant to this.

此外,您可能想查看本德威关于 COM 线程的链接,因为它与此非常相关。

回答by Mark Nold

Good question, but i think it can't be done.

好问题,但我认为这是不可能的。

回答by MichaelS

How about using something like ShellOpen() to start a shell script, e.g Visual Basic Script x times, which will do the job, communicating with the script through a file (and a pooling mechanism to detect when results arrive)? I think it is easier to do than writing a COM component. Also, VB Script is very similar to VBA. Bad sides are quite a few, though - writing and reading a file is more time consuming than sharing memory, pooling might make the VBA script seem unresponsive, etc.

如何使用像 ShellOpen() 这样的东西来启动一个 shell 脚本,例如 Visual Basic 脚本 x 次,它会完成这项工作,通过文件与脚本通信(以及检测结果何时到达的池机制)?我认为这比编写 COM 组件更容易。此外,VB Script 与 VBA 非常相似。不过,坏的一面也不少——写入和读取文件比共享内存更耗时,池化可能会使 VBA 脚本看起来没有响应等。

回答by Ionut

You can also use the DoEvents procedure which lets the system handle the events. Just call this sub from time to time and the main thread shoudn't freeze.

您还可以使用让系统处理事件的 DoEvents 过程。只需不时调用此子程序,主线程就不会冻结。

回答by dkretz

The quickest way I can think of is to create a Form and put an existing or do-it-yourself control on it.

我能想到的最快方法是创建一个表单并在其上放置一个现有的或自己动手的控件。

回答by andrewrk

If MS Access VBA lets you use forms, drop a timer on a form and set the delay to a really low value, like 10 ms. Then place your code in the timer event function, and it will be executed in a separate thread.

如果 MS Access VBA 允许您使用表单,请在表单上放置一个计时器并将延迟设置为非常低的值,例如 10 毫秒。然后将您的代码放在计时器事件函数中,它将在单独的线程中执行。