CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Help

  1. #1
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Help

    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



    Nicolas Bohemier

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Help

    In your stop button click set ret <> WAIT_TIMEOUT...


  3. #3
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Help

    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.

    Nicolas Bohemier

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Help

    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










  5. #5
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Help

    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



    Nicolas Bohemier

  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Help

    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.


  7. #7
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Help

    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.


  8. #8
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Help

    I guess VB is limiting me here.. I'll have to deal with that.. Thanks for your help

    Nicolas

    Nicolas Bohemier

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured