Home > Microsoft .Net Development Tips > ARCHIVE: Tips & Tricks > Wait...WAIT! Suppose your VB app has to wait
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ARCHIVE: TIPS & TRICKS

Wait...WAIT! Suppose your VB app has to wait


Derek Brown
10.17.2000
Rating: -3.60- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Wait...WAIT! Suppose Your VB App Has to Wait

SearchVB reader Derek E. Brown sends in this tip that should be very useful in a number of situations that can arise. The tip involves making a VB app wait for the completion of some other process outside VB. While Derek has a specific purpose in mind for this one, it should be fairly easy to modify for a number of different situations.

I found this to be an extremely useful piece of code for running an application from within VB and having the VB app wait until the outside process is complete. I have a program that has to wait until an uploaded zip file is unzipped before continued processing can take place. The code below involves a few API's, and though it's rather long and involved, I have yet to get an error using it.

'**************************************
'Windows API/Global Declarations for :Un
'zipper
'**************************************
Private Const ZIPSIG = &H4034B50
Private Const ziputil = "c:Program FilesPKUnzipPkzip25.exe"
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
'API's to monitor the status of Pkzip

Private Declare Function OpenProcess Lib "kernel32" (ByVal DesiredAccess As
Long, _ ByVal bInheritHandle As Long, ByVal ProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle
As Long, _ ByVal Milliseconds As Long) As Long Private Declare Function
CloseHandle Lib "kernel32"
(ByVal hObject As Long) As Long Private Type ZFHeader Signature As Long End Type

***********************************
Function fn_UnZipQReportFile() As Boolean
Dim FileToUnzip As String
Dim int_FilNum As Integer
Dim zhdr As ZFHeader
On Error Goto ErrorHandler:
fn_UnZipQReportFile = False
int_FilNum = 1

FileToUnzip = "C:Test.zip"

If FSO.FileExists(FileToUnzip) Then
Open FileToUnzip For Binary Access Read As #int_FilNum
Get #int_FilNum, , zhdr
If zhdr.Signature = ZIPSIG Then
If Array(int_ArrayCount).Password = "" Then
Err.Description = "No password supplied. File will Not unzip
If password protected. & _ Check database For empty (null) report password."
Goto ErrorHandler Else CmdLine = ziputil & " -ext -pass=" &
Array(int_ArrayCount).Password & " -dir=full -over=all " & FileToUnzip &
" " & Array(int_ArrayCount).Path
End If RetVal = Shell(CmdLine, vbMinimizedNoFocus)
If RetVal <> 0 Then sb_WaitForTemination (RetVal)
fn_UnZipQReportFile = True Else
Err.Description = "Zip file doesn't exist, " & FileToUnzip &
" " Goto ErrorHandler End If Else
Err.Description = "Zip file is empty, " & FileToUnzip & " "
Goto ErrorHandler End If
End If Exit Function
ErrorHandler: Call sub_Error_Log 'A seperate sub to write to an error log.
Resume Next
End Function
*************************************************
Private Sub sb_WaitForTemination(RetVl As Integer)
On Error Goto ErrorHandler:
Dim phnd&
phnd = OpenProcess(SYNCHRONIZE, 0, RetVl)

If phnd <> 0 Then
Call WaitForSingleObject(phnd, INFINITE)
Call CloseHandle(phnd)
End If
Exit Sub
ErrorHandler:
Err.Source = "sb_WaitForTermination in class Unzip in UnZipper"
Call sub_Error_Log
Resume Next
End Sub

Thanks, Derek. We'll be sending you a SearchVB denim shirt as a thank you for your submission. If you have a different solution for this problem, be sure to let us know. You can send your solution to us at tips@searchvb.com.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
ARCHIVE: Tips & Tricks
SearchVB wants your tips
Find out what VS.NET modules are installed
Retrieving icons from an EXE-file
Toggling Boolean values
VS.NET multiple tabbed windows
Set the value of a DataField in code behind
Easy grid design
Open using last Visual Studio.NET layout
Save development time by using inherited forms
Reusing code in VS.NET

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts