using System; using System.Collections.Generic; using System.Threading; using Interop.VixCOM; namespace Vestris.VMWareLib { /// /// A VixCOM job. /// Implements synchronous execution of VixCOM tasks. /// public class VMWareJob : VMWareVixHandle { private VMWareJobCallback _callback; /// /// A VMWare job created with a job completion callback. /// /// An instance of IJob. /// Job completion callback. public VMWareJob(IJob job, VMWareJobCallback callback) : base(job) { _callback = callback; // API-level errors aren't surfaced and the callback wait will never be set bool completedImmediately = false; VMWareInterop.Check(job.CheckCompletion(out completedImmediately)); } /// /// Wait for the job to complete, timeout. /// /// Timeout in seconds. public void Wait(int timeoutInSeconds) { _callback.WaitForCompletion(timeoutInSeconds * 1000); VMWareInterop.Check(_handle.WaitWithoutResults()); } /// /// Wait for the job to complete, return a result. /// /// Properties array. /// Timeout in seconds. /// Type of the property to return. /// Job result. public T Wait(object[] properties, int timeoutInSeconds) { _callback.WaitForCompletion(timeoutInSeconds * 1000); return (T)Wait(properties); } /// /// Wait for the job to complete and enumerate results. /// /// Properties to yield. /// Timeout in seconds. /// A results enumerator. public IEnumerable YieldWait(object[] properties, int timeoutInSeconds) { _callback.WaitForCompletion(timeoutInSeconds * 1000); for (int i = 0; i < GetNumProperties((int)properties[0]); i++) { yield return GetNthProperties(i, properties); } } /// /// Wait for the job to complete, return a result. /// /// Properties to yield. /// Property index to yield. /// Timeout in seconds. /// Type of the property to return. /// Job result. public T Wait(object[] properties, int index, int timeoutInSeconds) { _callback.WaitForCompletion(timeoutInSeconds * 1000); return (T)Wait(properties)[index]; } /// /// Wait for the job to complete, return a single result. /// /// Property id. /// Timeout in seconds. /// Type of property to return. /// A single job result. public T Wait(int propertyId, int timeoutInSeconds) { object[] properties = { propertyId }; return Wait(properties, 0, timeoutInSeconds); } /// /// Wait for the job to complete, return a result. /// /// Properties to return. /// Type of results. /// A job result. private T Wait(object[] properties) { object result = null; VMWareInterop.Check(_handle.Wait(properties, ref result)); return (T) result; } /// /// Get n-th properties. /// /// Type of result. /// Property index. /// Property objects. /// N'th properties. public T GetNthProperties(int index, object[] properties) { object result = null; VMWareInterop.Check(_handle.GetNthProperties(index, properties, ref result)); return (T)result; } /// /// Get the number of property values returned by the job. /// /// Property ID. /// The number of property values returned by the job. public int GetNumProperties(int property) { return _handle.GetNumProperties(property); } } }