Click to See Complete Forum and Search --> : Help


Boumxyz2
September 17th, 2001, 09:19 AM
Here's the thing.. I've been looking into this for 2 days and haven't find something on the net about that ( maybe I'm not looking at the right places ).

I'm writing a VB application that is watching a directory. As soon as a file gets in the directory, it is processed. While looking in the directory, it is in wait mode.

There's a button start and a button stop on the form. The start button start the process of watching the directory. Still I want to let the user stop it. So far so good.

But the thing is when you click stop It goes in cmdStop_click ( ) which is fine. In that I can stop all the waiting processes ( destroy the handles on it ) ..

The problem is that I go back in the sub where I was waiting. Is there any way that when you click stop it destroys the handle for the wait and then get out of the Sub where I was waiting ?

If you don't understand here's a little example


Sub cmdstart_click ()

' Code before wait
do while ret = WAIT_TIMEOUT
do events
ret = waitforsingleobject(handle)
' code get back here after stop but could get
' back anywhere after or before the wait..
' I just want to get out of that sub once it
' click stop. Don't wanna use a go to unless
' there are no other option
loop

' code after wait

End sub

sub cmdStop_click()

' Kill handles on wait objects

' Need to get out of caller.
end sub




Thanks in advance

Nicolas

DSJ
September 17th, 2001, 10:07 AM
In your stop button click set ret <> WAIT_TIMEOUT...

Boumxyz2
September 17th, 2001, 10:12 AM
Hum.. It will get out of my loop but it doesn't get me out of the Sub.. Which is what I wanna do.

If the user click stop I don't want to proceed with the rest of the sub.

DSJ
September 17th, 2001, 10:42 AM
No matter what, I think there will have to be a delay the length of your timeout value... is this closer to what you need?


option Explicit
Dim bStop as Boolean

Sub cmdstart_click()
' Code before wait
Do While ret = WAIT_TIMEOUT And Not bStop
DoEvents
ret = waitforsingleobject(Handle)
' code get back here after stop but could get
' back anywhere after or before the wait..
' I just want to get out of that sub once it
' click stop. Don't wanna use a go to unless
' there are no other option
If bStop then Exit Sub
Loop
If bStop then Exit Sub
' code after wait
End Sub
Sub cmdStop_click()
bStop = true
' Kill handles on wait objects
' Need to get out of caller.
End Sub

Boumxyz2
September 17th, 2001, 10:53 AM
This is closer to what I need but..

I don't want to test for the flag every instruction...

Is there any way to raise an error in cmdStop_click() and then catch it in cmdstart_click().

Like in C++

example

int cmdStart_click ()
{
try {
// waiting here
WaitforSingleObject();
}

catch {
// Do the error handling here

return;
}
}

int cmdStop_Click()
{
// Do stop code here
throw (error);
}


Thanks for your help anyway...

Nicolas

DSJ
September 17th, 2001, 11:14 AM
Sorry... VB error handling ain't quite that fancy... you have to handle the error within the sub/function that "throws" it... In my prior example you would only have to check for bStop immediately after your DoEvents however, because that is the only time the cmdStop_Click Sub would ever get run. If your timeout value is a very big you should probably also have a DoEvents after your api call and and check the bStop value there as well.

DSJ
September 17th, 2001, 11:22 AM
Another possible solution to this problem would be to use the WaitForMultipleObjects and exit the loop when either happened and in your cmdStop_Click signal the second event, but you will still need the "if xxx then exit sub" code to end the cmdStart_Click sub.

Boumxyz2
September 17th, 2001, 12:50 PM
I guess VB is limiting me here.. I'll have to deal with that.. Thanks for your help

Nicolas