设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3463|回复: 6
打印 上一主题 下一主题

Pausing Code to Wait Until a Shelled Process Is Finished in Access 95-2002

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2005-8-5 19:59:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
邀请网友翻译如下文章

标题:Pausing Code to Wait Until a Shelled Process Is Finished in Access 95-2002

作者: Attac Consulting Group



Here's how to shell to another program from Access, stop your code while the shelled process operates and then resume your code once the process is finished. To do this you use the api functions "WaitforSingleObject", and "OpenProcess" to launch a shelled process and wait for it to complete. Listed below is the code to use.

1. On the declarations page of your module, add the following functions: Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _

     dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _

     As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _

      hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _

      hObject As Long) As Long



2. Try out this test function, which launches any app you want to and waits until it is finished to display a message box (Note an " _ " underscore means line continuation): Function LaunchApp32 (MYAppname As String) As Integer

On Error Resume Next

Const SYNCHRONIZE = 1048576

Const INFINITE = -1&

Dim ProcessID&

Dim ProcessHandle&

Dim Ret&



LaunchApp32=-1

ProcessID = Shell(MyAppName, vbNormalFocus)

   If ProcessID<>0 then

       ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID&)

       Ret = WaitForSingleObject(ProcessHandle, INFINITE)

       Ret = CloseHandle(ProcessHandle)

  

       MsgBox "This code waited to execute until " _

          & MyAppName & " Finished",64

   Else

        MsgBox "ERROR : Unable to start " & MyAppname

        LaunchApp32=0

   End If

End Function

3. It is important to note that your function must include the code to close the process handle after the shelled application is complete, otherwise you will have a memory leak until you shut down Windows.

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2005-8-9 16:57:00 | 只看该作者
标题:在Access 95-2002中,如何使代码的执行时中断,并启动一个外部程序直至完成后,恢复中断。

作者: Attac顾问组织

    以下是如何从access启动另一个外部程序,使原来执行代码中断,直至这个外部程序完成后,恢复中断。完成这个功能我们可以"WaitforSingleObject"和"OpenProcess" API函数来启动这个外部程序直至完成。请参考下面列出代码。

1.在您模块的声明页中,加上以下声明和过程:





Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _

     dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _

     As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _

      hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _

      hObject As Long) As Long

2.请试验下面这个测试过程,它可是实现任何您想启动的一个外部应用程序直至完成,才弹出一个提示框。(“_”表示续行。)

Function LaunchApp32 (MYAppname As String) As Integer

On Error Resume Next

Const SYNCHRONIZE = 1048576

Const INFINITE = -1&

Dim ProcessID&

Dim ProcessHandle&

Dim Ret&



LaunchApp32=-1

ProcessID = Shell(MyAppName, vbNormalFocus)

   If ProcessID<>0 then

       ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID&)

       Ret = WaitForSingleObject(ProcessHandle, INFINITE)

       Ret = CloseHandle(ProcessHandle)

  

       MsgBox "This code waited to execute until " _

          & MyAppName & " Finished",64

   Else

        MsgBox "ERROR : Unable to start " & MyAppname

        LaunchApp32=0

   End If

End Function







值得注意的一点是,当这个外部应用程序完成后,在你的过程中必需包含一段代码用以关闭这个进程的句柄。否则内存将会溢出(即:内存死区)直到你关闭窗口为止。[em01][em01][em01]



[此贴子已经被作者于2005-8-18 15:42:12编辑过]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
3#
发表于 2005-8-12 06:37:00 | 只看该作者
厉害
4#
发表于 2008-5-19 11:28:20 | 只看该作者
看过
5#
发表于 2008-5-19 11:35:42 | 只看该作者
如果在path后面做个按钮选择路径和文件更好!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 10:29 , Processed in 0.085721 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表