Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement a "Cancel" button while a lenghty process is running

Status
Not open for further replies.

VB400

Programmer
Sep 8, 1999
359
US
<br>I would like to provide the users with the ability to cancel out of a long running process.<br><br>In a simple desktop application, this is somewhat simple -- inside the loop that is causing the lengthy process, add a DoEvents() function which yields control to the Windows environment.&nbsp;&nbsp;This will allow the cmdCancel_Click event to be processed.<br><br>However, in my case, the lenghty process is occurring on a completely different machine (client/server environment).&nbsp;&nbsp;For example, the code looks something like this:<br><br>Set objRemote = CreateObject(&quot;RemoteApp.RemoteClass, strServerName)<br>objRemote.PerformLenghtyProcess<br>Set objRemote = Nothing<br><br>Just before the method call, I would like to display a window that will display a &quot;please wait...&quot; type of message with a Cancel button.&nbsp;&nbsp;If the user presses the Cancel button, then I would like to stop the .PerformLenghtyProcess method.<br><br>Is this possible?<br><br>Thanks in advance!
 
You could put a doevents loop just after the call, then check check a variable that you set in the 'Cancel' button event.&nbsp;&nbsp;Then kill the process accordingly.&nbsp;&nbsp;(I'm not sure the api to use to kill the process, but the OLE server might have a Quit method, which would be cleaner).&nbsp;&nbsp;You might also look into the API WaitForSingleObject, inside a doevents loop.&nbsp;&nbsp;<br>--Jim
 
<br>Jim,<br><br>The problem is that once I make a call to the server object (i.e. objRemote.PerformLengthyProcess), control remains at that statement until it is finished.&nbsp;&nbsp;Adding a DoEvents loop after it, did not seem to help as control never got to the loop until the process was actually finished.<br><br>Can you elaborate on the WaitForSingleObject thought -- maybe there is something there!<br><br>Thanks!
 
You need to establish asyncronous communication between the two components.&nbsp;&nbsp;With your current architecture,&nbsp;&nbsp;you have synchronous communication, which means the function call must complete before control returns to the calling program. <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
<br>Until Microsoft comes up with a App.Communication = vbAsychronous &lt;grin&gt;, I will need some more help<br><br>Thanks!
 
VB400,<br>Below is how the api is used for Shell, which is Asynchronous.&nbsp;&nbsp;I now see your issue though,&nbsp;&nbsp;but I'm not sure how to call the ole server in async mode.&nbsp;&nbsp;I'd have to guess that there is indeed some setting like App.Communication = vbAsychronous...&nbsp;&nbsp;This might be in the registry, but I'm not sure.&nbsp;&nbsp;The bad news may be that the creator of the server object may set this at compile time...I don't know enough about that to speak with any certainty, though.&nbsp;&nbsp;Anyway, heres an example of WaitForSingleObject...<br><br><br>Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>Const SYNCHRONIZE = 1048576<br>Dim ProcessId As Long, ProcessHandle As Long, Ret<br><br>ProcessId = Shell(PgmName , vbNormalFocus)<br>ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessId)<br>Do<br>&nbsp;&nbsp;&nbsp;&nbsp;Ret = WaitForSingleObject (ProcessHandle, 100)<br>&nbsp;&nbsp;&nbsp;&nbsp;DoEvents<br>&nbsp;&nbsp;&nbsp;&nbsp;If CancelVar = True Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'app.quit or Kill the shelled process<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit do<br>&nbsp;&nbsp;&nbsp;&nbsp;end if<br>Loop While Ret = 258
 
<br>Thanks Jim,<br><br>I copied this code and added it to my personal arsenal.&nbsp;&nbsp;Although it won't help me in this scenario, I do have use for it in a different application.<br><br>Here's what I've learned so far -- since the lengthy process is happening on a different machine, I cannot control the &quot;stoppage&quot; of the process using DoEvents.&nbsp;&nbsp;However, if I start the remote process in an &quot;Asynchronous&quot; manner then I can put that call in a DoEvents loop (as you described above) which will allow me to &quot;interrupt&quot; with the Cancel button.<br><br>Did I get that right?<br><br>I'm hoping that someone already accomplished this and is itching to tell others about it (-:<br><br>
 
VB400,<br><br>I thought that this was supported - and would be accomplished by using a call back procedure. JohnK in the DCOM forum would know.<br><br>Mike<br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Mike is right that a callback will do what you want.&nbsp;&nbsp;Components can also generate events.&nbsp;&nbsp;Either soloution will require the server component to be recoded.<br> <p>nick bulka<br><a href=mailto:nick@bulka.com>nick@bulka.com</a><br><a href= > </a><br>
 
<br>Thanks for the info regarding CallBacks -- I will look into that process right away and hopefully I can report back something positive.<br><br>Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top