/
VMJobCallback.cs
69 lines (62 loc) · 2.51 KB
/
VMJobCallback.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using VixCOM;
namespace VMWareCrash
{
/// <summary>
/// A job completion callback, used with <see cref="Vestris.VMWareLib.VMWareJob" />.
/// </summary>
public class VMWareJobCallback : ICallback
{
#region ICallback Members
private EventWaitHandle _jobCompleted = new EventWaitHandle(false, EventResetMode.ManualReset);
/// <summary>
/// Handle a Vix event.
/// Currently handles the job completion event, call WaitForCompletion to block with a timeout.
/// </summary>
/// <param name="job">an instance that implements IJob</param>
/// <param name="eventType">event type</param>
/// <param name="moreEventInfo">additional event info</param>
public void OnVixEvent(IJob job, int eventType, IVixHandle moreEventInfo)
{
switch (eventType)
{
case Constants.VIX_EVENTTYPE_JOB_COMPLETED:
_jobCompleted.Set();
break;
}
/*
* fix: close job object since we are done with this object
*
* note: this does not invalidate the original job object that specified this as its callback function
* i.e. IJob createSnapshotJob = vm.CreateSnapshot(...); createSnapshotJob is still valid and can be used
*
*/
((IVixHandle2)job).Close();
}
/// <summary>
/// Wait for completion of the job with a timeout.
/// </summary>
/// <param name="timeoutInMilliseconds">Timeout in milliseconds.</param>
/// <returns>True if job completed, false if timeout expired.</returns>
public bool TryWaitForCompletion(int timeoutInMilliseconds)
{
return _jobCompleted.WaitOne(timeoutInMilliseconds, false);
}
/// <summary>
/// Wait for completion of the job with a timeout.
/// A <see cref="System.TimeoutException" /> occurs if the job hasn't completed within the timeout specified.
/// </summary>
/// <param name="timeoutInMilliseconds">Timeout in milliseconds.</param>
public void WaitForCompletion(int timeoutInMilliseconds)
{
if (!TryWaitForCompletion(timeoutInMilliseconds))
{
throw new TimeoutException();
}
}
#endregion
}
}