Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Views/3.3/SlaveCmdsWithKill.cs @ 6834

Last change on this file since 6834 was 6823, checked in by ascheibe, 13 years ago

#1233

  • implemented administrator ui review comments
  • implemented slave ui review comments
File size: 6.5 KB
RevLine 
[5314]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[5795]22using System;
[6257]23using System.Diagnostics;
24using System.Runtime.InteropServices;
[5826]25using System.Security.Principal;
26using System.ServiceProcess;
[5314]27using System.Windows.Forms;
[6456]28using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;
[5314]29using HeuristicLab.Common;
30using HeuristicLab.MainForm;
31
[5599]32namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
[5314]33
[6734]34  [View("HeuristicLab Slave Cmds with Kill View")]
[6730]35  [Content(typeof(SlaveItem), IsDefaultView = false)]
36  public partial class SlaveCmdsWithKill : SlaveCmdsBase {
[6734]37    private string serviceName = Settings.Default.ServiceName;
[5314]38
[6263]39    private const UInt32 BCM_SETSHIELD = 0x160C;
40    [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
41    static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);
42
[6730]43
[5314]44    public new SlaveItem Content {
45      get { return (SlaveItem)base.Content; }
[5320]46      set {
47        if (base.Content != value) {
48          base.Content = value;
49        }
50      }
[5314]51    }
52
[6730]53    public SlaveCmdsWithKill() {
[5314]54      InitializeComponent();
[5826]55
[6263]56      if (CheckRunAsAdmin()) {
57        btnKill.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
[6823]58        btnKill.Image = HeuristicLab.Common.Resources.VSImageLibrary.Stop;
[6263]59      } else {
60        this.btnKill.FlatStyle = FlatStyle.System;
61        SendMessage(btnKill.Handle, BCM_SETSHIELD, 0, (IntPtr)1);
62      }
[5314]63    }
64
65    #region Register Content Events
66    protected override void DeregisterContentEvents() {
[5320]67      base.DeregisterContentEvents();
[5314]68    }
69
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72    }
73    #endregion
74
[6730]75    protected override void Content_SlaveDisplayStateChanged(object sender, EventArgs<SlaveDisplayStat> e) {
76      base.Content_SlaveDisplayStateChanged(sender, e);
[6257]77
78      if (e.Value == SlaveDisplayStat.Asleep || e.Value == SlaveDisplayStat.NoService) {
79        btnKill.Enabled = false;
80      }
81
82      if (e.Value == SlaveDisplayStat.Busy || e.Value == SlaveDisplayStat.Idle || e.Value == SlaveDisplayStat.Offline) {
83        btnKill.Enabled = true;
84      }
85    }
86
[6730]87    protected override void Content_CoreConnectionChanged(object sender, EventArgs<CoreConnection> e) {
88      base.Content_CoreConnectionChanged(sender, e);
[6257]89      if (e.Value == CoreConnection.Offline) {
90        btnKill.Enabled = false;
91      }
92    }
[5315]93
[5826]94    private bool CheckRunAsAdmin() {
95      bool isRunAsAdmin = false;
96      WindowsIdentity user = WindowsIdentity.GetCurrent();
97      WindowsPrincipal principal = new WindowsPrincipal(user);
98
99      try {
100        isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
101      }
102      catch { }
103      return isRunAsAdmin;
104    }
105
[6263]106    /// <summary>
[6371]107    /// Shows the windows UAC dialog and restarts tray icon app
[6263]108    /// </summary>
[6257]109    private void ElevateApplication() {
[6371]110      // launch itself as administrator
[6456]111      ProcessStartInfo proc = new ProcessStartInfo(Application.ExecutablePath, Settings.Default.ShowUICmd);
[6257]112      proc.UseShellExecute = true;
113      proc.WorkingDirectory = Environment.CurrentDirectory;
114      proc.FileName = Application.ExecutablePath;
115      proc.Verb = "runas";
116
117      try {
118        Process.Start(proc);
119      }
120      catch {
[6263]121        // user refused to allow privileges elevation       
[6257]122        return;
123      }
[6263]124      Application.Exit();
[6257]125    }
126
127    private void StartService() {
[5826]128      TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
129
130      ServiceController service = new ServiceController(serviceName);
131      try {
132        if (service.Status == ServiceControllerStatus.Running) {
133          service.Stop();
134          service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
135        }
136
137        service.Start();
138        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
139      }
140      catch (InvalidOperationException ex) {
[6004]141        MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
[5826]142      }
143      catch (Exception ex) {
[6004]144        MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
[5826]145      }
146    }
[6257]147
148    private void StopService() {
[6263]149      TimeSpan timeout = TimeSpan.FromMilliseconds(7000);
[6257]150
151      ServiceController service = new ServiceController(serviceName);
152      try {
153        if (service.Status == ServiceControllerStatus.Running) {
154          service.Stop();
155          service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
156        }
157      }
158      catch (InvalidOperationException ex) {
159        MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
160      }
161      catch (Exception ex) {
162        MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
163      }
164    }
165
[6730]166    protected override void OnContentChanged() {
167      base.OnContentChanged();
168      btnKill.Enabled = false;
169    }
170
171    protected override void SetEnabledStateOfControls() {
172      base.SetEnabledStateOfControls();
173    }
174
[6257]175    private void btnKill_Click(object sender, EventArgs e) {
176      if (CheckRunAsAdmin()) {
177        StopService();
178      } else {
179        ElevateApplication();
180      }
181    }
182
[6730]183    protected override void btnStart_Click(object sender, EventArgs e) {
[6257]184      if (Content != null) {
185        if (lastSlaveDisplayStat == SlaveDisplayStat.Asleep) {
186          Content.RestartCore();
187        } else if (lastSlaveDisplayStat == SlaveDisplayStat.NoService) {
188          if (CheckRunAsAdmin()) {
189            StartService();
190          } else {
191            ElevateApplication();
192          }
193        }
194      }
195    }
[5314]196  }
197}
Note: See TracBrowser for help on using the repository browser.