flyingleaf's blog

象外行一样思考,象专家一样实践。

VB6 Async File Find Component

coolbean | 10 九月, 2007 02:02

Download code:

VB6_Async_File_Find_Component.zip(10K)

Multi-Threaded VB Using ActiveX EXEs

coolbean | 10 九月, 2007 01:42

Run code asynchronously with this simple and elegant design model

No more Hourglasses

Ever since VB5 service pack 2, there has been the possibility of running VB applications multi-threaded by using ActiveX EXEs. However, if you try and research this you will find it is fiddly to get working. This article presents a method which makes running operations asynchronously really easy.

Multi-Threading

Although ActiveX EXEs can run in a new thread, I've never really understood how to get it working: you need to use CreateObject to make the object on a new thread, and then you need to be very careful as to how you start the work you want to do asynchronously. Most times you find that VB blocks until the method call is complete regardless of whether the object is in a new thread or not.

What you want to be able to do is to say I want to perform some operation asynchronously and I would like to be notified when it is complete. This project demonstrates a tiny code module and a type library you can add into your own ActiveX projects to do just that. It is based around a part of the MSDN "CodeFlow sample" (see downloads).

Asynchronous Running - Just to get it Started

The main problem with getting a multi-threaded application up and running is how to call a method in VB without the caller being blocked. It turns out there is a simple solution to this problem. The steps are as follows:

  1. Put the method call into an ActiveX EXE, and make it private to the EXE.
  2. Provide a new wrapper method to call the method you want to run asynchronously. This wrapper method enables a timer. When the timer fires, the original method is called.

This solves the problem because the method you call immediately yields control back to the caller, and then it is left to Windows pre-emptive multi-tasking to raise the timer event and kick off the process within the ActiveX EXE. There is no further interference because the ActiveX EXE is running in a different process to the caller.

In More Detail

To stop having to have a form in the ActiveX EXE, this solution is based on a Win32 API timer. Win32 API timers come in two flavours: either they notify the application when they tick by posting a WM_TIMER message to a window, or they fire a callback interface. This solution uses the callback interface, and as a consequence must be implemented within a module (because VB will not provide the address of a function to callback to for any function within an object, only one in a module).

Because the code to start the object is implemented in a module, the module must have a reference to the object instance it has to start when the timer fires. To achieve this without the possibility for errors, an interface is defined that the object can implement and the module will only use this for communication. In this sample, the interface is defined in a Type Library called Runnable. This allows you to reference the Type Library without having to declare it as a public class from the ActiveX executable.

The final implementation feature is the use of the OLE/COM API call CoLockObjectExternal to ensure that the object being started asynchronously isn't inadvertently terminated by the caller before the timer has had a chance to be fired.

Here is the code in the mStart.bas module:

 (查看全文)

How to disable combobox control via API

coolbean | 05 九月, 2007 23:51

EnableWindow hwnd,0

Note: EnableWindow function sends WM_CANCELMODE to the combobox control.

Vista SP1 2008年第一季度发布

coolbean | 02 九月, 2007 15:02

微软宣布了Vista SP1的发布计划。再过几周,Vista SP1的beta测试就将展开,但只有少数人(15000人)能在九月底参与beta测试。之后,MSDN和TechNet的订阅者可获得SP1的预先发布版。正式发布则定于2008年初。SP1没有加入新功能,主要是安全性、可靠性、性能上的改进。 Geek.com就此采访了微软Vista产品经理Nick White,谈论了SP1对用户的意义。对于用户是否会等到SP1发布之后再去更新他们的操作系统,White指出Windows更新程序已让service pack的重要性降低,实际上用户并没有必要等待。

Python3000来了

coolbean | 02 九月, 2007 15:00

Python3000的第一个alpha版本终于来了,在未来还要release多个测试版本,最终计划于08年8月发布正式版。
这里了解更多。
«上一篇   1 2
 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb