Real Software Forums
http://forums.realsoftware.com/

wait for launch to complete
http://forums.realsoftware.com/viewtopic.php?f=6&t=47102
Page 1 of 1

Author:  dharding [ Wed Feb 27, 2013 12:14 pm ]
Post subject:  wait for launch to complete

Is there a way for an RB application (or a thread of it) to wait for an f.Launch to complete?

This external application could put some data in the clipboard, depending on what the user does, and I want to check the clipboard for this data when the external application closes.

Author:  charonn0 [ Wed Feb 27, 2013 12:38 pm ]
Post subject:  Re: wait for launch to complete

Look in the Examples folder in the RB install directory for the Windows Functionality Suite. In the WFS there's a function called LaunchAndWait that does exactly what it's name says.

Note that your entire application will stop running until LaunchAndWait is done waiting (i.e. the other app exits.)

Author:  dharding [ Wed Feb 27, 2013 1:27 pm ]
Post subject:  Re: wait for launch to complete

I'm looking in the following path:

C:\Program Files (x86)\Real Software\Real Studio 2012r2.1\Example Projects

I'm not seeing a Windows Functionality Suite.

I also saw the following path, but also did not find it there:

C:\Program Files (x86)\Real Software\Real Studio 2012r2.1\Example Projects\Platform-Specific\Windows

Author:  charonn0 [ Wed Feb 27, 2013 2:17 pm ]
Post subject:  Re: wait for launch to complete

For me, it's here:
C:\Program Files (x86)\REAL Software\Real Studio 2011r4.3\Example Projects\Windows OS specific\Windows Functionality Suite 2.5.

Here's the function I'm talking about:

Sub LaunchAndWait(extends f as FolderItem, params as String = "", deskName as String = "")
// We want to launch the application and wait for
// it to finish executing before we return.
#if TargetWin32
Soft Declare Function CreateProcessW Lib "Kernel32" ( appName as WString, params as WString, _
procAttribs as Integer, threadAttribs as Integer, inheritHandles as Boolean, flags as Integer, _
env as Integer, curDir as Integer, startupInfo as Ptr, procInfo as Ptr ) as Boolean

Soft Declare Function CreateProcessA Lib "Kernel32" ( appName as CString, params as CString, _
procAttribs as Integer, threadAttribs as Integer, inheritHandles as Boolean, flags as Integer, _
env as Integer, curDir as Integer, startupInfo as Ptr, procInfo as Ptr ) as Boolean

dim startupInfo, procInfo as MemoryBlock

startupInfo = new MemoryBlock( 17 * 4 )
procInfo = new MemoryBlock( 16 )

dim unicodeSavvy as Boolean = System.IsFunctionAvailable( "CreateProcessW", "Kernel32" )

startupInfo.Long( 0 ) = startupInfo.Size

dim deskStr, deskPtr as MemoryBlock
if deskName <> "" then
if unicodeSavvy then
deskStr = ConvertEncoding( deskName + Chr( 0 ), Encodings.UTF16 )
else
deskStr = deskName + Chr( 0 )
end if

startupInfo.Ptr( 8 ) = deskStr
end if

dim ret as Boolean
if unicodeSavvy then
ret = CreateProcessW( f.AbsolutePath, params, 0, 0, false, 0, 0, 0, startupInfo, procInfo )
else
ret = CreateProcessA( f.AbsolutePath, params, 0, 0, false, 0, 0, 0, startupInfo, procInfo )
end if

if not ret then return

Declare Function WaitForSingleObject Lib "Kernel32" ( handle as Integer, howLong as Integer ) as Integer
Const INFINITE = -1
Const WAIT_TIMEOUT = &h00000102
Const WAIT_OBJECT_0 = &h0

// We want to loop here so that we can yield time back
// to other threads. This means threaded applications
// will "just work", but non-threaded ones will still appear hung.
while WaitForSingleObject( procInfo.Long( 0 ), 1 ) = WAIT_TIMEOUT
App.SleepCurrentThread( 10 )
wend

Declare Sub CloseHandle Lib "Kernel32" ( handle as Integer )
CloseHandle( procInfo.Long( 0 ) )
CloseHandle( procInfo.Long( 4 ) )
#endif
End Sub


Call it like this (e.g.):

Dim f As FolderItem = SpecialFolder.Desktop.Child("TestFile.exe")
f.LaunchAndWait()
'Or:
f.LaunchAndWait("--optional-command-line-params")

Author:  dharding [ Wed Feb 27, 2013 2:35 pm ]
Post subject:  Re: wait for launch to complete

Thanks. I went back and installed RB20114.3 - (must be discontinued) and found it in the place you specified. Thanks for the tip. I can see in the code that it should work if I call this from a thread as it will only "hang" that thread.

-Dan

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/