VIX API Concepts : Callback Functions
2012-10-15 16:42
162 查看
Callback Functions
All asynchronous Vix functions, such as VixVM_Open(), include a parameter for a callback procedure and a parameterthat is passed to the callback procedure. These parameters are optional, so a caller can pass NULL for either. The prototype of this callback procedure parameter is:
typedef void (VixEventProc)(VixHandle handle, VixEventType eventType, VixHandle moreEventInfo, void *clientData);
Using a Callback
Function
If the caller provides a callback procedure, that procedure is registered with the job object and is invoked when the job object is signaled. For example, if a caller passesa callback procedure to VixVM_Open(), that callback procedure is invoked when the virtual machine has been opened. This situation could happen either before or after VixVM_Open() returns.
It also can happen on any thread.
This mechanism allows the Vix function to complete asynchronously, so the application should not call VixJob_Wait() when
using a callback function.
When a callback procedure is invoked, it is passed the clientData parameter that was passed in the original call to
the asynchronous function. This allows a callback procedure to associate some context with an outstanding asynchronous call.
Example 2-14.
void myCallback(VixHandle jobHandle, VixEventType eventType, VixHandle moreEventInfo, void *clientData) { VixError err; VixError asyncErr; VixHandle vmHandle = VIX_INVALID_HANDLE; /* * Ignore progress callbacks. Check only for final signal. */ if (VIX_EVENTTYPE_JOB_COMPLETED != eventType) { return; } err = Vix_GetProperties(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &vmHandle, VIX_PROPERTY_JOB_RESULT_ERROR_CODE, &asyncErr, VIX_PROPERTY_NONE); if (VIX_OK != asyncErr) { /* * The open failed. */ } } int main() { VixError err = VIX_OK; VixHandle hostHandle = VIX_INVALID_HANDLE; VixHandle jobHandle = VIX_INVALID_HANDLE; VixHandle vmHandle = VIX_INVALID_HANDLE; char *contextData = "Hello, Vix"; jobHandle = VixHost_Connect(VIX_API_VERSION, VIX_SERVICEPROVIDER_VMWARE_VI_SERVER, "https://server2.example.com/sdk", // hostName 0, // hostPort "root", // username "secretpw", // password 0, // options VIX_INVALID_HANDLE, // propertyListHandle NULL, // callbackProc NULL); // clientData // Block for host connection to complete. err = VixJob_Wait(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &hostHandle, VIX_PROPERTY_NONE); Vix_ReleaseHandle(jobHandle); jobHandle = VIX_INVALID_HANDLE; if (VIX_OK != err) { goto abort; } // Use callback function to capture completion of virtual machine open. jobHandle = VixVM_Open(hostHandle, "[standard] WindowsXP/WindowsXP.vmx", myCallback, contextData); /* * Do something, like pump a message pump. * Later, myCallback will be invoked on another thread. */ abort: Vix_ReleaseHandle(jobHandle); jobHandle = VIX_INVALID_HANDLE; }
Callback Events
Note that a callback might be called several times, for several different reasons. For example, it might be called for periodic progress updates. The eventType parameterindicates why the callback is being called. The supported event types are:
VIX_EVENTTYPE_JOB_COMPLETED -- This event indicates that the asynchronous action has completed, whether successfully
or not.
VIX_EVENTTYPE_JOB_PROGRESS -- This event may be passed several times to report progress on an asynchronous action.
VIX_EVENTTYPE_FIND_ITEM -- This event is used by VixHost_FindItems().
VIX_EVENTTYPE_HOST_INITIALIZED -- This event is used by VixHost_Connect().
相关文章推荐
- VIX API Concepts : Objects and Handles
- VIX API Concepts : Vix_ReleaseHandle
- VIX API Concepts : VixError
- VIX API Concepts : Handle Properties
- VIX API Concepts : Monitoring a Job Object
- VIX API Concepts : Event Models
- Listing of the NT Native API functions
- The Concept of Callbacks
- VIX API学习报告
- RTAI API-----Message handling functions
- Delphi中用MessageBox()API函数做倒计时对话框(使用Hook安装CBTHookCallback,计时器更改文字,SetWindowText API真正修改文字,引用未知函数)good
- jq.contentShown 的 API, Callback参数,data格式和build格式说明。
- Fusioncharts图表组件API参考方法(Functions)汇总篇(续)
- LwIP - raw/callback API、协议栈API(sequential API)、BSD API(或者说 SOCKET API)
- Use year() and month() functions in NHibernate Criteria API
- JQuery Callback and Functions
- UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT
- Spark PairRDDFunctions[K,V]聚合相关的API
- MQTT的学习研究(十四) MQTT moquette 的 Callback API 消息发布订阅的实现
- BW--API functions available in BEX 3.x