VMware {code} Community
AlexBreu
Contributor
Contributor

How to wait a task after cloning VM with VMWare.vim.dll ?

Hello everybody,

I work on a web interface which manage virtual machines on vsphere Esx 5.5. My interface is based on .net web forms (not MVC)

I followed jeffpaton posts (using VMware.Vim  ) which helped me (Thanks to you, Jeff).

But now i freeze on this subject. I do not know how to wait a task after cloning VM. I develop a little web page which launch vsphere command to my vsphere Esc with vmware.vim. I need to know when vpshere is finished his work to launch an another instruction.

I try to use PropertyCollector but i do not know how to use it :

i read this post but without success :

Synchronous versus Asynchronous Calls in vSphere API | DoubleCloud => Private Cloud + Public Cloud

What is the best way to wait for a Task in a concurrent environment?

Here is my code. i use jeffpaton functions.

...

using VMware.Vim;

...

VimClient client;

string serverUrl = "..."

client.Connect("https://" + serverUrl + "/sdk");

client.Login(userLogin, userPassword);

...

ManagedObjectReference cloneTask_MoRef = null;

cloneTask_MoRef = sourceVm.cloneVM_Task(sourceVm.Parent, "cloneName", mySpec);

if (cloneTask_MoRef == null) {

     //error

}else

     {

          PropertyCollector pc = new PropertyCollector(client, cloneTask_MoRef);

          //1 PropertyFilterSpec [] pfs;

          //ObjectSpec

          ObjectSpec objectSpec = new ObjectSpec();

                          

          //PropertySpec

          //What could i do to know if the task is over or in progress ? 😞

I need some help.

Thanks for all.                                                          

1 Reply
petermie
Enthusiast
Enthusiast

I know this is super old, and hopefully you have since figured it out but I also struggled with this. At first I wrote the following function and called it in a loop to get status but I ended up ultimately moving away from cloneVM_Task to just cloneVM as it waits so I didn't have to worry about the looping function calls.

protected bool isTaskRunning(VimClient vimClient, ManagedObjectReference taskMoRef)

        {

            try

            {

                Task task = (Task)vimClient.GetView(taskMoRef, null);

                if (task != null)

                {

                    TaskInfo itmTaskInfo = task.Info;

                    if (itmTaskInfo.State == TaskInfoState.running)

                    { return true; }

                    else { return false; }

                }

                else

                {

                    return false;

                }

            }

            catch(Exception e)

            {

               //errorMessage is a property of my class that I can check for errors on a return of false

                errorMessage = e.Message.ToString();

                return false;

            }

        }