VirtualBox

Changeset 24915 in vbox for trunk


Ignore:
Timestamp:
Nov 24, 2009 3:09:43 PM (15 years ago)
Author:
vboxsync
Message:

VBoxManage: Implemented cancelation in showProgress for cancelable progress objects.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManage.cpp

    r24912 r24915  
    4242#include <VBox/version.h>
    4343
     44#include <iprt/asm.h>
    4445#include <iprt/buildconfig.h>
    45 #include <iprt/env.h>
    4646#include <iprt/initterm.h>
    4747#include <iprt/stream.h>
    4848#include <iprt/string.h>
    4949
     50#include <signal.h>
     51
    5052#include "VBoxManage.h"
    5153
     
    5456*   Global Variables                                                           *
    5557*******************************************************************************/
    56 /*extern*/ bool g_fDetailedProgress = false;
    57 
     58/*extern*/ bool         g_fDetailedProgress = false;
    5859
    5960#ifndef VBOX_ONLY_DOCS
     61/** Set by the signal handler. */
     62static volatile bool    g_fCanceled = false;
     63
     64
     65/**
     66 * Signal handler that sets g_fCanceled.
     67 *
     68 * This can be executed on any thread in the process, on Windows it may even be
     69 * a thread dedicated to delivering this signal.  Do not doing anything
     70 * unnecessary here.
     71 */
     72static void showProgressSignalHandler(int iSignal)
     73{
     74    NOREF(iSignal);
     75    ASMAtomicWriteBool(&g_fCanceled, true);
     76}
     77
     78
    6079/**
    6180 * Print out progress on the console
     
    84103    }
    85104
     105    /* setup signal handling if cancelable */
     106    bool fCanceledAlready = false;
     107    BOOL fCancelable;
     108    HRESULT hrc = progress->COMGETTER(Cancelable)(&fCancelable);
     109    if (FAILED(hrc))
     110        fCancelable = FALSE;
     111    if (fCancelable)
     112    {
     113        signal(SIGINT,   showProgressSignalHandler);
     114#ifdef SIGBREAK
     115        signal(SIGBREAK, showProgressSignalHandler);
     116#endif
     117    }
     118
    86119    while (SUCCEEDED(progress->COMGETTER(Completed(&fCompleted))))
    87120    {
     
    101134            }
    102135
    103             if (    (ulCurrentPercent != ulLastPercent)
    104                  || (ulCurrentOperationPercent != ulLastOperationPercent)
     136            if (    ulCurrentPercent != ulLastPercent
     137                 || ulCurrentOperationPercent != ulLastOperationPercent
    105138               )
    106139            {
     
    133166            break;
    134167
     168        /* process async cancelation */
     169        if (g_fCanceled && !fCanceledAlready)
     170        {
     171            hrc = progress->Cancel();
     172            if (SUCCEEDED(hrc))
     173                fCanceledAlready = true;
     174            else
     175                g_fCanceled = false;
     176        }
     177
    135178        /* make sure the loop is not too tight */
    136179        progress->WaitForCompletion(100);
     180    }
     181
     182    /* undo signal handling */
     183    if (fCancelable)
     184    {
     185        signal(SIGINT,   SIG_DFL);
     186#ifdef SIGBREAK
     187        signal(SIGBREAK, SIG_DFL);
     188#endif
    137189    }
    138190
     
    143195        if (SUCCEEDED(iRc))
    144196            RTPrintf("100%%\n");
     197        else if (g_fCanceled)
     198            RTPrintf("CANCELED\n");
    145199        else
    146200            RTPrintf("FAILED\n");
     
    151205    return iRc;
    152206}
     207
    153208#endif /* !VBOX_ONLY_DOCS */
    154 
    155209
    156210int main(int argc, char *argv[])
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette