CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2024
    Posts
    5

    win32 - How to interrupt execution

    Hi,
    this seems to be a common task - but I have no idea how to set about, nor could I find any helpful information in the windows manuals.

    When my code enters in a lengthy evaluation of data, I want to be able to abort the execution of this loop. I have a dialog with a progress bar and a cancel-Button. But I cannot click this button while the process is running - so I cannot interrupt or abort the process. Here is my code (I am using fortran, if there is a forum on this site better suited for my problem, please let me know):

    Code:
                hProgress = CreateDialog(NULL, IDD_PROGRESS, hWnd,  loc(DN_PrgrsDlgProc)) 
                cRslt = 'Schneller Vorlauf 'C
                iRslt = SetDlgItemText (hProgress, IDC_PROGRESSTEXT, cRslt)
                iRslt = UpdateWindow (hProgress)
                hBar = GetDlgItem (hProgress, IDC_PROGRESSBAR)
                iNSteps = (iRunEnd - iiSimZeit) / iiTimeStep
                iCount = 0   
                hAbort= GetDlgItem(hProgress, IDCANCEL)       
                do 
                    iiSimZeit = iiSimZeit + iiTimeStep
                    if (iiSimZeit .gt. 0) iRslt = DN_Update()       ! t = 0 wird von InitSim gef?llt, daher 0 ?berspringen!
                    if (iiSImZeit .gt. iRunEnd) exit
                    iRslt = DN_SetLoad ()                           ! EEG-Erzeugungen bestimmen
                    iRslt = DN_EuroDispo ()                         ! Strom-Verteilung in Europa                  
                    iRslt = DN_AutoDispatch()                       ! Residuallast ausgleichen
                    irslt = DN_Eval()                               ! Netzstr?me errechnen
                    !iRslt = DN_Redispatch()                        ! Knotenlasten, d.h. Kraftwerke und Kreise an die Netzkapazit?ten anpassen  
                    iRslt = DN_SumUp ()                             ! Daten zusammenstellen
                    nRun = nRun + 1
                    iRslt = DN_OutComes(nRun)                       ! Ergebnisse ausgeben
                    iCount = iCount + 1
                    iRslt = iCount * 100 / iNSteps
                    iRslt = SendMessage (hBar, PBM_SETPOS, iRslt, 0)
                enddo
                lRslt = DestroyWindow (hProgress)
    The loop runs for several minutes and I want to be able to stop it without stopping the application. I my mind this boils down to the question how I can make the cancel-button to react to mouse input. Once this is possible I may be able to check the state or process messages in my process-dialog-procedure.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,415

    Re: win32 - How to interrupt execution

    The usual way to work with a lengthy calculations etc. with the possibility to cancel it is putting these lengthy calculations in a worker thread.
    Unfortunately I don't know if it's possible in FORTRAN (since I don't use it for about 30 years.)
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: win32 - How to interrupt execution

    For an article on using worker threads with c, see:
    http://www.flounder.com/workerthreads.htm
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2024
    Posts
    5

    Re: win32 - How to interrupt execution

    Oh, I almost feared that this might not be as easy as it looks.
    Thanks to you both, I will try my luck with threading

    N.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: win32 - How to interrupt execution

    The 'cheat quick and dirty' way is to use PeekMessage() in the calculation loop and exit the calculation if required messages are in the message queue. This will slow down the calculation loop (probably substantially). But much better to do it 'properly' using a worker thread.

    https://learn.microsoft.com/en-us/wi...r-peekmessagea
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2024
    Posts
    5

    Re: win32 - How to interrupt execution

    Quote Originally Posted by 2kaud View Post
    The 'cheat quick and dirty' way is to use PeekMessage() in the calculation loop and exit the calculation if required messages are in the message queue. This will slow down the calculation loop (probably substantially). But much better to do it 'properly' using a worker thread.

    https://learn.microsoft.com/en-us/wi...r-peekmessagea
    That is an idea. I could set a counter to send this message once for say every 500 cycles, say for a time of about one second. This would be a reasonable reponse time without too much burden in performance.

    Thanks

    N.A

  7. #7
    Join Date
    Oct 2024
    Posts
    1

    Re: win32 - How to interrupt execution

    Hi there!

    I understand the challenge you're facing with handling long-running tasks in Fortran. One effective approach to allow for user interaction, such as clicking the cancel button, is to implement a separate thread for your lengthy evaluations. By doing so, you can keep your GUI responsive while the processing occurs.

    **Links removed by Site Administrator so it doesn't look like you're spamming us. Please don't post them again.**

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