using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Interop.VixCOM;
namespace Vestris.VMWareLib
{
///
/// A job completion callback, used with .
///
public class VMWareJobCallback : ICallback
{
#region ICallback Members
private EventWaitHandle _jobCompleted = new EventWaitHandle(false, EventResetMode.ManualReset);
///
/// Handle a Vix event.
///
/// An instance that implements IJob.
/// Event type.
/// Additional event info.
public void OnVixEvent(IJob job, int eventType, IVixHandle moreEventInfo)
{
using(VMWareVixHandle jobHandle = new VMWareVixHandle(job))
{
using (VMWareVixHandle moreEventInfoHandle = new VMWareVixHandle(moreEventInfo))
{
OnVixEvent(jobHandle, eventType, moreEventInfo);
}
}
}
///
/// Handle a Vix event.
/// Currently handles the job completion event, call WaitForCompletion to block with a timeout.
///
/// An instance that implements IJob.
/// Event type.
/// Additional event info.
private void OnVixEvent(VMWareVixHandle job, int eventType, IVixHandle moreEventInfo)
{
switch (eventType)
{
case Constants.VIX_EVENTTYPE_JOB_COMPLETED:
_jobCompleted.Set();
break;
}
}
///
/// Wait for completion of the job with a timeout.
///
/// Timeout in milliseconds.
/// True if job completed, false if timeout expired.
public bool TryWaitForCompletion(int timeoutInMilliseconds)
{
return _jobCompleted.WaitOne(timeoutInMilliseconds, false);
}
///
/// Wait for completion of the job with a timeout.
/// A occurs if the job hasn't completed within the timeout specified.
///
/// Timeout in milliseconds.
public void WaitForCompletion(int timeoutInMilliseconds)
{
if (!TryWaitForCompletion(timeoutInMilliseconds))
{
throw new TimeoutException(string.Format("The operation has timed out after {0} milliseconds.", timeoutInMilliseconds));
}
}
#endregion
}
}