22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . IO ;
5+ using System . Linq ;
56using System . Text ;
7+ using System . Threading ;
68using Microsoft . Win32 ;
79
810//using Vestris.VMWareLib;
@@ -14,6 +16,7 @@ public class VMware : Host {
1416 protected string mDir ;
1517 protected string mVmxPath ;
1618 protected Process mProcess ;
19+ protected Process [ ] mProcessesBefore , mProcessesAfter , mProcesses ;
1720 protected string mWorkstationPath ;
1821 protected string mPlayerPath ;
1922 protected string mHarddisk ;
@@ -89,7 +92,27 @@ public override void Start() {
8992 xPSI . UseShellExecute = false ; //must be true to allow elevate the process, sometimes needed if vmware only runs with admin rights
9093 mProcess . EnableRaisingEvents = true ;
9194 mProcess . Exited += ExitCallback ;
95+
96+ mProcessesBefore = Process . GetProcessesByName ( "vmware-vmx" ) ;
9297 mProcess . Start ( ) ;
98+
99+ Stopwatch watch = Stopwatch . StartNew ( ) ;
100+
101+ // Wait for the process to spawn
102+ mProcessesAfter = Process . GetProcessesByName ( "vmware-vmx" ) ;
103+ while ( mProcessesAfter . Length == mProcessesBefore . Length )
104+ {
105+ if ( watch . Elapsed . Seconds == 15 )
106+ {
107+ watch . Stop ( ) ;
108+ throw new TimeoutException ( "VMware Workstation took too long to launch!" ) ;
109+ }
110+
111+ mProcessesAfter = Process . GetProcessesByName ( "vmware-vmx" ) ;
112+ }
113+
114+ // Get the new processes
115+ mProcesses = mProcessesBefore . Concat ( mProcessesAfter ) . Distinct ( ) . ToArray ( ) ;
93116 }
94117
95118 private void ExitCallback ( object sender , EventArgs e )
@@ -113,11 +136,11 @@ public override void Stop() {
113136 {
114137 //TODO: Close VMWare properly
115138
116- //Force Kill VMWare
139+ // Kill the VMware GUI
117140 mProcess . Kill ( ) ;
118141
119- //kil vmware-vmx.exe
120- foreach ( var process in Process . GetProcessesByName ( "vmware-vmx.exe" ) )
142+ // Kill the actual VM instance
143+ foreach ( var process in mProcesses )
121144 {
122145 process . Kill ( ) ;
123146 }
0 commit comments