~cszikszoy/do-plugins/vbox

« back to all changes in this revision

Viewing changes to VirtualBox/src/VMItem.cs

  • Committer: chris at szikszoy
  • Date: 2008-11-07 23:19:29 UTC
  • Revision ID: chris@szikszoy.com-20081107231929-az8szy1ysn0m3amh
fixed race condition where VM state wasn't stored properly

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
                                ico_file = string.Format("VirtualBox_64px.png@{0}", GetType().Assembly.FullName);
67
67
                        }
68
68
                        name = MachineInfo[0].Attributes["name"].Value;
69
 
                        //determine the state of thte VM
70
 
                        ProcessStartInfo ps = new ProcessStartInfo ("VBoxManage", "showvminfo " + uuid);
71
 
                        ps.UseShellExecute = false;
72
 
                        ps.RedirectStandardOutput = true;
73
 
                        using (Process p = Process.Start (ps)) {
74
 
                                p.WaitForExit ();
75
 
                                string output = p.StandardOutput.ReadToEnd ();
76
 
                                int s = output.IndexOf("State:");
77
 
                                int e = output.IndexOf("\n", s);
78
 
                                string outputState = output.Substring(s, e-s);
79
 
                                //States: saved, running, paused, powered off
80
 
                                if (outputState.Contains("saved"))
81
 
                                        state = VMState.saved;
82
 
                                else if (outputState.Contains("running"))
83
 
                                        state = VMState.on;
84
 
                                else if (outputState.Contains("paused"))
85
 
                                        state = VMState.paused;
86
 
                                else if (outputState.Contains("powered off"))
87
 
                                        state = VMState.off;
88
 
                                if (output.Contains("Snapshots:"))
89
 
                                    has_saved_states = true;
90
 
                        }                   
 
69
                        
 
70
                        this.state = CurrentState;
 
71
                }
 
72
                
 
73
                public VMState CurrentState
 
74
                {
 
75
                        get
 
76
                        {
 
77
                                VMState cur_state = default(VMState);
 
78
                                Console.WriteLine("Updating State for {0}", this.name);
 
79
                                //determine the state of thte VM
 
80
                                ProcessStartInfo ps = new ProcessStartInfo ("VBoxManage", "showvminfo " + uuid);
 
81
                                ps.UseShellExecute = false;
 
82
                                ps.RedirectStandardOutput = true;
 
83
                                using (Process p = Process.Start (ps))
 
84
                                {
 
85
                                        p.WaitForExit ();
 
86
                                        string output = p.StandardOutput.ReadToEnd ();
 
87
                                        int s = output.IndexOf("State:");
 
88
                                        int e = output.IndexOf("\n", s);
 
89
                                        string outputState = output.Substring(s, e-s);
 
90
                                        //States: saved, running, paused, powered off
 
91
                                        if (outputState.Contains("saved"))
 
92
                                                cur_state = VMState.saved;
 
93
                                        else if (outputState.Contains("running"))
 
94
                                                cur_state = VMState.on;
 
95
                                        else if (outputState.Contains("paused"))
 
96
                                                cur_state = VMState.paused;
 
97
                                        else if (outputState.Contains("powered off"))
 
98
                                                cur_state = VMState.off;
 
99
                                        if (output.Contains("Snapshots:"))
 
100
                                            has_saved_states = true;
 
101
                                }
 
102
                                return cur_state;
 
103
                        }
91
104
                }
92
105
                
93
106
                public string Name { get { return name; } }