Changeset 6730 for branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Views/3.3/SlaveStats.cs
- Timestamp:
- 09/09/11 20:19:43 (13 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Views/3.3/SlaveStats.cs
r6727 r6730 21 21 22 22 using System; 23 using System.Diagnostics;24 using System.Runtime.InteropServices;25 using System.Security.Principal;26 using System.ServiceProcess;27 using System.Threading;28 using System.Threading.Tasks;29 23 using System.Windows.Forms; 30 24 using System.Windows.Forms.DataVisualization.Charting; 31 using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;32 25 using HeuristicLab.Common; 33 26 using HeuristicLab.Core.Views; … … 37 30 namespace HeuristicLab.Clients.Hive.SlaveCore.Views { 38 31 39 [View("HeuristicLab Slave View")]40 [Content(typeof(SlaveItem), IsDefaultView = true)]41 public partial class Slave View: ItemView {32 [View("HeuristicLab Slave Stats View")] 33 [Content(typeof(SlaveItem), IsDefaultView = false)] 34 public partial class SlaveStats : ItemView { 42 35 private SlaveDisplayStat lastSlaveDisplayStat; 43 private const string serviceName = "HeuristicLab.Clients.Hive.SlaveCore.SlaveWindowsService";44 36 45 private const UInt32 BCM_SETSHIELD = 0x160C;46 [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]47 static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);48 37 49 38 public new SlaveItem Content { … … 56 45 } 57 46 58 public Slave View() {47 public SlaveStats() { 59 48 InitializeComponent(); 60 61 if (CheckRunAsAdmin()) {62 btnKill.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;63 btnKill.Image = HeuristicLab.Common.Resources.VSImageLibrary.BreakpointActive;64 } else {65 this.btnKill.FlatStyle = FlatStyle.System;66 SendMessage(btnKill.Handle, BCM_SETSHIELD, 0, (IntPtr)1);67 }68 69 49 lblSlaveState.Text = SlaveDisplayStat.NoService.ToString(); 70 50 lastSlaveDisplayStat = SlaveDisplayStat.NoService; … … 74 54 #region Register Content Events 75 55 protected override void DeregisterContentEvents() { 76 Content.SlaveShutdown -= new System.EventHandler(Content_SlaveShutdown);77 56 Content.SlaveStatusChanged -= new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged); 78 57 Content.SlaveDisplayStateChanged -= new EventHandler<EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged); … … 85 64 base.RegisterContentEvents(); 86 65 87 Content.SlaveShutdown += new System.EventHandler(Content_SlaveShutdown);88 66 Content.SlaveStatusChanged += new System.EventHandler<EventArgs<StatusCommons>>(Content_SlaveStatusChanged); 89 67 Content.SlaveDisplayStateChanged += new EventHandler<EventArgs<SlaveDisplayStat>>(Content_SlaveDisplayStateChanged); … … 94 72 protected override void OnContentChanged() { 95 73 base.OnContentChanged(); 96 btnKill.Enabled = false;97 btnStart.Enabled = false;98 btnStop.Enabled = false;99 100 if (Content == null) {101 //nothing to do...102 } else {103 //try to establish a connection to the slave service104 if (base.Content != null) {105 ((SlaveItem)base.Content).Open();106 Task.Factory.StartNew(Connector);107 }108 }109 }110 111 private void Connector() {112 Content_SlaveDisplayStateChanged(this, new EventArgs<SlaveDisplayStat>(SlaveDisplayStat.NoService));113 bool connected = false;114 while (!connected) {115 this.Invoke(new Func<bool>(() => connected = ((SlaveItem)base.Content).ReconnectToSlaveCore()));116 117 if (!connected) {118 Thread.Sleep(1000);119 }120 }121 this.Invoke(new Action(SetEnabledStateOfControls));122 74 } 123 75 … … 133 85 } 134 86 135 void Content_SlaveShutdown(object sender, System.EventArgs e) {136 Task.Factory.StartNew(Connector);137 }138 139 87 void Content_SlaveDisplayStateChanged(object sender, EventArgs<SlaveDisplayStat> e) { 140 88 lblSlaveState.Text = e.Value.ToString(); 141 89 lastSlaveDisplayStat = e.Value; 142 143 if (e.Value == SlaveDisplayStat.Asleep || e.Value == SlaveDisplayStat.NoService) {144 btnKill.Enabled = false;145 btnStart.Enabled = true;146 btnStop.Enabled = false;147 }148 149 if (e.Value == SlaveDisplayStat.Busy || e.Value == SlaveDisplayStat.Idle || e.Value == SlaveDisplayStat.Offline) {150 btnKill.Enabled = true;151 btnStart.Enabled = false;152 btnStop.Enabled = true;153 }154 90 } 155 91 156 92 void Content_CoreConnectionChanged(object sender, EventArgs<CoreConnection> e) { 157 if (e.Value == CoreConnection.Offline) { 158 btnKill.Enabled = false; 159 btnStart.Enabled = false; 160 btnStop.Enabled = false; 161 } 93 162 94 } 163 95 #endregion … … 228 160 coresChart.Series[0].Points.Add(pUsedCores); 229 161 } 230 231 private bool CheckRunAsAdmin() {232 bool isRunAsAdmin = false;233 WindowsIdentity user = WindowsIdentity.GetCurrent();234 WindowsPrincipal principal = new WindowsPrincipal(user);235 236 try {237 isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);238 }239 catch { }240 return isRunAsAdmin;241 }242 243 /// <summary>244 /// Shows the windows UAC dialog and restarts tray icon app245 /// </summary>246 private void ElevateApplication() {247 // launch itself as administrator248 ProcessStartInfo proc = new ProcessStartInfo(Application.ExecutablePath, Settings.Default.ShowUICmd);249 proc.UseShellExecute = true;250 proc.WorkingDirectory = Environment.CurrentDirectory;251 proc.FileName = Application.ExecutablePath;252 proc.Verb = "runas";253 254 try {255 Process.Start(proc);256 }257 catch {258 // user refused to allow privileges elevation259 return;260 }261 Application.Exit();262 }263 264 private void StartService() {265 TimeSpan timeout = TimeSpan.FromMilliseconds(5000);266 267 ServiceController service = new ServiceController(serviceName);268 try {269 if (service.Status == ServiceControllerStatus.Running) {270 service.Stop();271 service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);272 }273 274 service.Start();275 service.WaitForStatus(ServiceControllerStatus.Running, timeout);276 }277 catch (InvalidOperationException ex) {278 MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());279 }280 catch (Exception ex) {281 MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());282 }283 }284 285 private void StopService() {286 TimeSpan timeout = TimeSpan.FromMilliseconds(7000);287 288 ServiceController service = new ServiceController(serviceName);289 try {290 if (service.Status == ServiceControllerStatus.Running) {291 service.Stop();292 service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);293 }294 }295 catch (InvalidOperationException ex) {296 MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());297 }298 catch (Exception ex) {299 MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());300 }301 }302 303 private void btnKill_Click(object sender, EventArgs e) {304 if (CheckRunAsAdmin()) {305 StopService();306 } else {307 ElevateApplication();308 }309 }310 311 private void btnStop_Click(object sender, EventArgs e) {312 if (Content != null) {313 Content.Sleep();314 }315 }316 317 private void btnStart_Click(object sender, EventArgs e) {318 if (Content != null) {319 if (lastSlaveDisplayStat == SlaveDisplayStat.Asleep) {320 Content.RestartCore();321 } else if (lastSlaveDisplayStat == SlaveDisplayStat.NoService) {322 if (CheckRunAsAdmin()) {323 StartService();324 } else {325 ElevateApplication();326 }327 }328 }329 }330 162 } 331 163 }
Note: See TracChangeset
for help on using the changeset viewer.