Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

Best way to yield execution

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best way to yield execution

    Hello All,

    Is PeekMessage(msg, hwnd, 0, 0, %PM_NOREMOVE) the best way to yield execution so that the operating system can process other events?

    I have a very large text file to process which may take about a minute to complete the task and I want the user to be able to press a cancel button and my app to handle the message.

    Any ideas?


    Pat

    PS: I use SDK programming style - button created using CreateWindow().

  • #2
    Actually the best way to do this specific thing is to process the text file in a separate thread of execution; this allows your primary user interface to receive and processes messages.

    If one of those messages is "Abort" , you use something like this demo here...

    Terminate Worker Threads Using Windows Events (and Using Thread Local Storage) Demo Dec 23 2005

    (Yes, 'best' is subjective).

    MCM
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Thanks Michael,

      That's a nice example which I'll dissect later on.

      The issue for me is that this is one-off for a client - it just process a multi megabyte text file and I want to give the user a way of cancelling the process.

      So would PeekMessage() do the trick in this instance. I want to use PB here whereas previously I would have used VB.

      (I'm afraid that I've been using VB alot for a few years now - mainly because of clients wishes - and my skill base has been depleted. Oh, age is also killing my memory.)


      Pat

      Comment


      • #4
        it just process a multi megabyte text file and I want to give the user a way of cancelling the process.
        Canceling the process v. Yielding to the system? You are matching apples and submarines.

        Or do you mean PeekMessage() in the "Process the text file" procedure? No, that won't work, because Peek'ing the message does not Process the message.

        Oh, well, today's an off-day. I think I'll do a demo for this, the right way. Goodness knows I've posted the pieces often enough.

        All I really gotta do is change the previously-posted example to add a GUI with a CANCEL button and enable/disable the controls appropriately. BUt I think I have a demo of that already somewhere, too.

        I'll keep it simple and well commented. I'll even use "DDT" even though you are SDK... I think it will be more useful to others that way.



        MCM
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          SLEEP 0 every couple of seconds might do the trick.
          How long is an idea? Write it down.

          Comment


          • #6
            Michael,

            Thanks for your assistance. However, it's a bit moot. I though it may take several minutes to process the 20 meg file and wanted some way for the user to cancel the process or at least allow other processes to function.

            However, PB will process the file in less than 15 seconds so I think my clumsy method will be OK. It’s also a one-off. Once the file’s processed the app wont be used again.

            Nonetheless, I need to refresh myself with how to do this properly. My SDK knowledge base is mired in 16 bit technology and while I did some work with threads years ago, I’ve forgotten both the method and detail.


            Pat

            Comment


            • #7
              Short of using a separate thread, the best way I've discovered is peekMessage.

              Code:
              DO WHILE peekMessage(lpMsg, 0, 0, 0, %PM_NOREMOVE) <> 0: DIALOG DOEVENTS 0: LOOP

              Comment


              • #8
                OK, if you can't understand what this demo does, you are "uncoachable"

                This is the best* way to handle a program requiring:
                User Interface + Worker Thread to process a file (or anything else) + Abort

                Compiled with PB/Windows 8.03 but should compile and run using PB/Win 7x, too.

                Only thing I am not sure about is how WM_SYSCOMMAND/SC_CLOSE will be handled by DDT in 7x. Near as I can tell, it should not have handled this the way I did it in 8x, but it works. (Don't allow close if worker thread running).

                http://www.powerbasic.com/support/pb...ad.php?t=35619

                MCM
                *subjective
                Last edited by Michael Mattias; 24 Nov 2007, 04:29 PM.
                Michael Mattias
                Tal Systems Inc. (retired)
                Racine WI USA
                [email protected]
                http://www.talsystems.com

                Comment

                Working...
                X