How to get notification for Completed task

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
AdityaM
Posts: 31
Joined: 8. Nov 2016, 06:12

How to get notification for Completed task

Post by AdityaM »

Hello,
I am using the VirtualBox API "IProgress::waitForCompletion()" and as a parameter I am passing it "-1" to wait for indefinite time. Is there a way to check when the process/task is completed, by some sort of flag or interrupts?
Thanks
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to get notification for Completed task

Post by noteirak »

Sadly, there isn't any callback or event listener available.

You will need to setup a dedicated thread waiting short amount of time (typically between 100 and 500 ms) to handle waiting and create custom events/callback when the progress amount changes.

This is especially important since you also need to process the event queue meanwhile.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
AdityaM
Posts: 31
Joined: 8. Nov 2016, 06:12

Re: How to get notification for Completed task

Post by AdityaM »

What if I try to get the values of following attributes from IProgress - percent, timeRemaining and completed, would I be able to achieve my objective?
Basically what I am trying to do is delete the Virtual Machine, and I am waiting indefinitely till the machine is Deleted (but on a custom UI I have to show a progress bar, so to display the progress bar/or a message on successful operation I need those events).

Here is the small snippet of the code that I have written, just for reference -

Code: Select all

IMachine *machine = NULL;
IProgress *progress = NULL;

long rc = machine->Unregister(mode,&resourceArray);

         if (!FAILED(rc))
         {
           rc = machine->DeleteConfig(resourceArray,&progress);

           if (!FAILED(rc))
           {
             rc = progress->WaitForCompletion (-1);
           }
           else
           {
               //do something
           }
         }
         else
         {
            //do something
         }
Last edited by socratis on 23. Nov 2016, 07:14, edited 1 time in total.
Reason: Enclosed the information in [code] tag for better readability
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to get notification for Completed task

Post by noteirak »

noteirak wrote:You will need to setup a dedicated thread waiting short amount of time (typically between 100 and 500 ms) to handle waiting and create custom events/callback when the progress amount changes.
So something like this (pseudo-java example):

Code: Select all

IMachine machine = vbMgr.getVBox().findMachine(uuid);
try {
    List<IMedium> hdds = machine.unregister(CleanupMode.DetachAllReturnHardDisksOnly);
    IProgress p = machine.deleteConfig(hdds);
    while (!p.getCompleted() || p.getCanceled()) {
        try {
            Thread.sleep(500); // 500ms, but you can set anything you like here, even adapt depending on remaining time to have a more dynamic update
            produceProgressUpdateEvent(p); // create your own custom event here to update your UI accordingly
        } catch (InterruptedException e) {
            Logger.exception(e);
        }
    }
    if (p.getResultCode() != 0) {
        throw new MachineException(p);
    }
} catch (VBoxException e) {
    throw ErrorInterpreter.transform(e);
}
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply