Results 1 to 8 of 8

Thread: [RESOLVED] How to cause Cancel with the Mouse w/o using Doevents?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Mn-USA
    Posts
    124

    Resolved [RESOLVED] How to cause Cancel with the Mouse w/o using Doevents?

    In my app, I'm in a very intensive cpu calculation loop(millions of calculations per loop). I have a Cancel button for the user to click on but I cannot get it to trigger an event unless I use doevents in the loop. I have set a cmdCancel.refresh with sleep 50 but it appears the mouse is not able to do a click(at least that's what it looks like to me). I can move the mouse over the cancel button ok whike running, So I'm thinking I need to let the mouse loose somehow.
    Any ideas how to get the cancel to function? It does not have to be immediate, just within a second after click.
    l
    There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!
    Richard P. Feynman

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,399

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    DoEvents may be what you need. However, you may need to have a counter that only executes a DoEvents after a certain number of calculations have been completed. You may need to experiment with counter values to see what value works without a big delay.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Mn-USA
    Posts
    124

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    Thanks. Unfortunately, loop time is variable depending on user input. All I want is to have the cmdCancel button to trigger when pressed.
    There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!
    Richard P. Feynman

  4. #4
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,399

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    In that case, to use DoEvents, you may need to add some code that checks the system time and executes a DoEvents after a certain number of seconds.

    Or, alternatively, check out the links below:

    http://www.vbforums.com/showthread.p...ve-to-DoEvents

    http://www.vbforums.com/showthread.p...faster-methods

    http://www.vbforums.com/showthread.p...place-DoEvents

  5. #5
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    460

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    Quote Originally Posted by VB-only View Post
    Thanks. Unfortunately, loop time is variable depending on user input. All I want is to have the cmdCancel button to trigger when pressed.
    DoEvents is pure evil but your case is a good reason why it exists. Just remember than any number of other events (key presses, clicks, etc) will also fire so if you do not block this, you may have a code that gets executed out of order and you may create bugs that are really hard to reproduce. I would recommend displaying a modal form with your cancel button and any messages\progress bars to help contain any unwanted events.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Location
    Mn-USA
    Posts
    124

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    Thanks for all the good ideas. Using the GetInputState function worked really well.
    My problem solved.
    There is a computer disease that anybody who works with computers knows about. It's a very serious disease and it interferes completely with the work. The trouble with computers is that you 'play' with them!
    Richard P. Feynman

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: [RESOLVED] How to cause Cancel with the Mouse w/o using Doevents?

    Yes, you must remember that VB6 is single-threaded. If you don't release the thread, no other code is going to execute until you do. You can refresh things, but that is not releasing the thread. Without getting into API calls and/or multi-threading (which isn't natively possible in VB6), DoEvents is your only alternative. It's the only clear way to get other code to run while you're in a long-running loop.

    I think of DoEvents just like calling a function. It goes to see if anything else needs to be done (such as firing events), and doesn't return to your loop until that's done. You just have to be careful (probably disabling certain controls on the first entry into your loop) that your loop isn't started more than once. But monitoring for a Cancel button and then setting some global "cancel" variable that's checked right after your DoEvents is the way long-running loops are canceled. And, when used judiciously in this way, there's nothing wrong with DoEvents.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: How to cause Cancel with the Mouse w/o using Doevents?

    Ahhh, I see you resorted to an API call. Yeah, you could certainly check for the state of the mouse. That's another good way to go.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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