public GuestOS GetGuestOS() { GuestOS os = null; if (this.Status == VMState.Enabled) { if (GuestOSParams == null) { IDictionary <string, string> kvps = null; try { kvps = Utility.GetKvpItems(this); } catch (Exception ex) { }; if (kvps != null) { string OSName = null; string OSVer = null; string csdver = null; string ip4Addr = string.Empty; if (kvps.ContainsKey("OSName")) { OSName = kvps["OSName"]; } if (kvps.ContainsKey("OSVersion")) { OSVer = kvps["OSVersion"]; } if (kvps.ContainsKey("CSDVersion")) { csdver = kvps["CSDVersion"]; } if (kvps.ContainsKey("NetworkAddressIPv4")) { ip4Addr = kvps["NetworkAddressIPv4"] ?? string.Empty; } if ((OSName != null) && (OSVer != null) && (csdver != null)) { os = new GuestOS(OSName, OSVer, csdver, ip4Addr); } } } } return(os); }
public VMDetailsViewModel(VM vm) : base(vm) { view = new VMDetailsView(); this.vmModel = VMModel.GetInstance(); this.IsActiveVM = true; //lock (vmModel.ActiveVMList) //{ // vmModel.ActiveVMList.Clear(); // vmModel.ActiveVMList.Add(this.VirtualMachine); //} VMName = this.VirtualMachine.Name; vmstatusString = this.VirtualMachine.StatusString; VMDomainName = this.VirtualMachine.GetDomainName(); GuestOS guestOS = this.VirtualMachine.GetGuestOS(); if (guestOS == null) { guestOS = new GuestOS(string.Empty, string.Empty, string.Empty, string.Empty); } OSName = guestOS.OSName; OSVersion = guestOS.OSVersion; CSDVersion = guestOS.CSDVersion; IPv4Address = guestOS.IPv4Address; VMSnapshot snapshotTree = this.VirtualMachine.SnapshotTree; if (snapshotTree != null) { VMSnapshots = new VMSnapshotViewModel[] { new VMSnapshotViewModel(snapshotTree, this.VirtualMachine, vmModel) }; } else { VMSnapshots = new VMSnapshotViewModel [] {}; } foreach (VMSnapshotViewModel snapshotVM in VMSnapshots) { snapshotVM.Selected += (o, e) => { this.SelectedSnapshotItem = snapshotVM.SnapshotInstance; }; } this.VirtualMachine.VMSnapshotsChanged += (o, e) => { snapshotTree = this.VirtualMachine.SnapshotTree; if (snapshotTree != null) { VMSnapshots = new VMSnapshotViewModel[] { new VMSnapshotViewModel(snapshotTree, this.VirtualMachine, vmModel) }; } }; ThreadStart backgroundThreadStart = new ThreadStart(() => { while (true) { GCHandle pinnedArray = GCHandle.Alloc(this.VirtualMachine.MediumThumbnailImage, GCHandleType.Pinned); IntPtr ptr = pinnedArray.AddrOfPinnedObject(); Bitmap BI = new Bitmap(160, 120, 320, System.Drawing.Imaging.PixelFormat.Format16bppRgb565, ptr); MemoryStream st = new MemoryStream(); BI.Save(st, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(st.ToArray()); bmp.EndInit(); bmp.Freeze(); ThumbnailImage = bmp; Thread.Sleep(2000); pinnedArray.Free(); } }); if ((this.VirtualMachine.Status != VMState.Unknown) && (this.VirtualMachine.Status != VMState.Disabled)) { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } this.VirtualMachine.VMStatusChanged += (o, e) => { this.VMName = this.VirtualMachine.Name; this.VMStatusString = this.VirtualMachine.GetStatusString(); this.VMDomainName = this.VirtualMachine.GetDomainName(); guestOS = this.VirtualMachine.GetGuestOS(); if (guestOS == null) { guestOS = new GuestOS(string.Empty, string.Empty, string.Empty, string.Empty); } OSName = guestOS.OSName; OSVersion = guestOS.OSVersion; CSDVersion = guestOS.CSDVersion; IPv4Address = guestOS.IPv4Address; if ((this.VirtualMachine.Status != VMState.Unknown) && (this.VirtualMachine.Status != VMState.Disabled)) { if (BackgroundThread != null) { if (!BackgroundThread.IsAlive) { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } } else { BackgroundThread = new Thread(backgroundThreadStart); BackgroundThread.IsBackground = true; BackgroundThread.Priority = ThreadPriority.Lowest; BackgroundThread.Start(); } } else { if (BackgroundThread != null) { BackgroundThread.Abort(); BackgroundThread.Join(10); } } }; view.SetViewModel(this); }