Code inspection cleanup

This commit is contained in:
2019-09-28 22:20:21 +01:00
parent b962420b94
commit e782ac36fb
15 changed files with 184 additions and 187 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ namespace ASCOM.Meade.net
Product = "", Copyright = "", Trademark = "",
AssemblyVersion = "", FileVersion = "", Guid = "",
NeutralLanguage = "";
public bool IsComVisible = false;
public bool IsComVisible;
// Return a particular assembly attribute value.
public static T GetAssemblyAttribute<T>(Assembly assembly)
+5 -5
View File
@@ -33,8 +33,8 @@ namespace ASCOM.Meade.net
#region Access to ole32.dll functions for class factories
// Define two common GUID objects for public usage.
private static readonly Guid _iidIUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
private static readonly Guid _iidIDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
private static readonly Guid IidIUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");
private static readonly Guid IidIDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
[Flags]
enum Clsctx : uint
@@ -118,7 +118,7 @@ namespace ASCOM.Meade.net
public ClassFactory(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
_mClassType = type;
//PWGS Get the ProgID from the MetaData
@@ -201,12 +201,12 @@ namespace ASCOM.Meade.net
//
// Handle requests for IDispatch or IUnknown on the class
//
if (riid == _iidIDispatch)
if (riid == IidIDispatch)
{
ppvObject = Marshal.GetIDispatchForObject(Activator.CreateInstance(_mClassType));
return;
}
else if (riid == _iidIUnknown)
else if (riid == IidIUnknown)
{
ppvObject = Marshal.GetIUnknownForObject(Activator.CreateInstance(_mClassType));
}
+37 -30
View File
@@ -29,7 +29,7 @@ namespace ASCOM.Meade.net
public static class Server
{
private const string DRIVER_NAME = "Meade Generic";
private const string DriverName = "Meade Generic";
#region Access to kernel32.dll, user32.dll, and ole32.dll functions
[Flags]
@@ -259,7 +259,7 @@ namespace ASCOM.Meade.net
catch (Exception e)
{
MessageBox.Show($"Failed to load served COM class assembly {fi.Name} - {e.Message}",
DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
DriverName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
@@ -287,19 +287,21 @@ namespace ASCOM.Meade.net
//
private static void ElevateSelf(string arg)
{
ProcessStartInfo si = new ProcessStartInfo();
si.Arguments = arg;
si.WorkingDirectory = Environment.CurrentDirectory;
si.FileName = Application.ExecutablePath;
si.Verb = "runas";
var si = new ProcessStartInfo
{
Arguments = arg,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Application.ExecutablePath,
Verb = "runas"
};
try { Process.Start(si); }
catch (System.ComponentModel.Win32Exception)
{
MessageBox.Show($"The {DRIVER_NAME} was not {(arg == "/register" ? "registered" : "unregistered")} because you did not allow it.", DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show($"The {DriverName} was not {(arg == "/register" ? "registered" : "unregistered")} because you did not allow it.", DriverName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
MessageBox.Show(ex.ToString(), DriverName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
return;
}
@@ -349,8 +351,8 @@ namespace ASCOM.Meade.net
//
// HKCR\APPID\exename.ext
//
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format("APPID\\{0}",
Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1))))
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(
$"APPID\\{Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)}"))
{
key.SetValue("AppID", _sAppId);
}
@@ -358,7 +360,7 @@ namespace ASCOM.Meade.net
catch (Exception ex)
{
MessageBox.Show($"Error while registering the server:\n{ex}",
DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
DriverName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
finally
@@ -381,7 +383,7 @@ namespace ASCOM.Meade.net
//PWGS Generate device type from the Class name
string deviceType = type.Name;
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(string.Format("CLSID\\{0}", clsid)))
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey($"CLSID\\{clsid}"))
{
key.SetValue(null, progid); // Could be assyTitle/Desc??, but .NET components show ProgId here
key.SetValue("AppId", _sAppId);
@@ -427,7 +429,7 @@ namespace ASCOM.Meade.net
catch (Exception ex)
{
MessageBox.Show("Error while registering the server:\n" + ex.ToString(),
DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
DriverName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
bFail = true;
}
finally
@@ -454,9 +456,9 @@ namespace ASCOM.Meade.net
//
// Local server's DCOM/AppID information
//
Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}", _sAppId), false);
Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}",
Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)), false);
Registry.ClassesRoot.DeleteSubKey($"APPID\\{_sAppId}", false);
Registry.ClassesRoot.DeleteSubKey(
$"APPID\\{Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)}", false);
//
// For each of the driver assemblies
@@ -472,17 +474,17 @@ namespace ASCOM.Meade.net
//
// HKCR\progid
//
Registry.ClassesRoot.DeleteSubKey(String.Format("{0}\\CLSID", progid), false);
Registry.ClassesRoot.DeleteSubKey($"{progid}\\CLSID", false);
Registry.ClassesRoot.DeleteSubKey(progid, false);
//
// HKCR\CLSID\clsid
//
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}\\Implemented Categories\\{{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}}", clsid), false);
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}\\Implemented Categories", clsid), false);
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}\\ProgId", clsid), false);
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}\\LocalServer32", clsid), false);
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}\\Programmable", clsid), false);
Registry.ClassesRoot.DeleteSubKey(String.Format("CLSID\\{0}", clsid), false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}\\Implemented Categories\\{{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}}", false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}\\Implemented Categories", false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}\\ProgId", false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}\\LocalServer32", false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}\\Programmable", false);
Registry.ClassesRoot.DeleteSubKey($"CLSID\\{clsid}", false);
try
{
//
@@ -494,7 +496,10 @@ namespace ASCOM.Meade.net
p.Unregister(progid);
}
}
catch (Exception) { }
catch (Exception)
{
// ignored
}
}
}
#endregion
@@ -515,7 +520,7 @@ namespace ASCOM.Meade.net
if (!factory.RegisterClassObject())
{
MessageBox.Show("Failed to register class factory for " + type.Name,
DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Stop);
DriverName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return false;
}
}
@@ -571,7 +576,7 @@ namespace ASCOM.Meade.net
default:
MessageBox.Show("Unknown argument: " + args[0] + "\nValid are : -register, -unregister and -embedding",
DRIVER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
DriverName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
}
@@ -610,9 +615,11 @@ namespace ASCOM.Meade.net
RegisterClassFactories();
// Start up the garbage collection thread.
GarbageCollection garbageCollector = new GarbageCollection(1000);
Thread gcThread = new Thread(new ThreadStart(garbageCollector.GcWatch));
gcThread.Name = "Garbage Collection Thread";
var garbageCollector = new GarbageCollection(1000);
var gcThread = new Thread(garbageCollector.GcWatch)
{
Name = "Garbage Collection Thread"
};
gcThread.Start();
//
+5 -5
View File
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms;
@@ -52,7 +53,7 @@ namespace ASCOM.Meade.net
comboBoxComPort.SelectedItem = profileProperties.ComPort;
}
txtGuideRate.Text = profileProperties.GuideRateArcSecondsPerSecond.ToString();
txtGuideRate.Text = profileProperties.GuideRateArcSecondsPerSecond.ToString(CultureInfo.CurrentCulture);
try
{
cboPrecision.SelectedItem = profileProperties.Precision;
@@ -86,7 +87,6 @@ namespace ASCOM.Meade.net
private void TextBox1_TextChanged(object sender, EventArgs e)
{
//const double SIDRATE = 0.9972695677; //synodic/solar seconds per sidereal second
try
{
double newGuideRate = double.Parse(txtGuideRate.Text.Trim());
@@ -103,17 +103,17 @@ namespace ASCOM.Meade.net
_guideRateValid = false;
}
UpdateOKButton();
UpdateOkButton();
}
private void UpdateOKButton()
private void UpdateOkButton()
{
cmdOK.Enabled = _guideRateValid && (comboBoxComPort.SelectedItem != null);
}
private void ComboBoxComPort_SelectedValueChanged(object sender, EventArgs e)
{
UpdateOKButton();
UpdateOkButton();
}
public void SetReadOnlyMode()
+24 -29
View File
@@ -15,6 +15,7 @@
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using ASCOM.Utilities;
@@ -64,7 +65,7 @@ namespace ASCOM.Meade.net
/// <summary>
/// number of connections to the shared serial port
/// </summary>
public static int Connections { get; set; } = 0;
public static int Connections { get; set; }
public static void SendBlind(string message)
{
@@ -179,7 +180,7 @@ namespace ASCOM.Meade.net
driverProfile.DeviceType = "Telescope";
driverProfile.WriteValue(DriverId, TraceStateProfileName, profileProperties.TraceLogger.ToString());
driverProfile.WriteValue(DriverId, ComPortProfileName, profileProperties.ComPort);
driverProfile.WriteValue(DriverId, GuideRateProfileName, profileProperties.GuideRateArcSecondsPerSecond.ToString());
driverProfile.WriteValue(DriverId, GuideRateProfileName, profileProperties.GuideRateArcSecondsPerSecond.ToString(CultureInfo.CurrentCulture));
driverProfile.WriteValue(DriverId, PrecisionProfileName, profileProperties.Precision);
}
}
@@ -264,9 +265,9 @@ namespace ASCOM.Meade.net
/// The Key is the connection number that identifies the device, it could be the COM port name,
/// USB ID or IP Address, the Value is the DeviceHardware class
/// </summary>
private static readonly Dictionary<string, DeviceHardware> _connectedDevices = new Dictionary<string, DeviceHardware>();
private static readonly Dictionary<string, DeviceHardware> ConnectedDevices = new Dictionary<string, DeviceHardware>();
private static readonly Dictionary<string, DeviceHardware> _connectedDeviceIds = new Dictionary<string, DeviceHardware>();
private static readonly Dictionary<string, DeviceHardware> ConnectedDeviceIds = new Dictionary<string, DeviceHardware>();
/// <summary>
@@ -278,17 +279,17 @@ namespace ASCOM.Meade.net
{
lock (LockObject)
{
if (!_connectedDevices.ContainsKey(deviceId))
_connectedDevices.Add(deviceId, new DeviceHardware());
_connectedDevices[deviceId].Count++; // increment the value
if (!ConnectedDevices.ContainsKey(deviceId))
ConnectedDevices.Add(deviceId, new DeviceHardware());
ConnectedDevices[deviceId].Count++; // increment the value
if (!_connectedDeviceIds.ContainsKey(driverId))
_connectedDeviceIds.Add(driverId, new DeviceHardware());
_connectedDeviceIds[driverId].Count++; // increment the value
if (!ConnectedDeviceIds.ContainsKey(driverId))
ConnectedDeviceIds.Add(driverId, new DeviceHardware());
ConnectedDeviceIds[driverId].Count++; // increment the value
if (deviceId == "Serial")
{
if (_connectedDevices[deviceId].Count == 1)
if (ConnectedDevices[deviceId].Count == 1)
{
var profileProperties = ReadProfile();
SharedSerial.PortName = profileProperties.ComPort;
@@ -308,8 +309,8 @@ namespace ASCOM.Meade.net
return new ConnectionInfo
{
Connections = _connectedDevices[deviceId].Count,
SameDevice = _connectedDeviceIds[driverId].Count
Connections = ConnectedDevices[deviceId].Count,
SameDevice = ConnectedDeviceIds[driverId].Count
};
}
}
@@ -318,12 +319,12 @@ namespace ASCOM.Meade.net
{
lock (LockObject)
{
if (_connectedDevices.ContainsKey(deviceId))
if (ConnectedDevices.ContainsKey(deviceId))
{
_connectedDevices[deviceId].Count--;
if (_connectedDevices[deviceId].Count <= 0)
ConnectedDevices[deviceId].Count--;
if (ConnectedDevices[deviceId].Count <= 0)
{
_connectedDevices.Remove(deviceId);
ConnectedDevices.Remove(deviceId);
if (deviceId == "Serial")
{
SharedSerial.Connected = false;
@@ -331,16 +332,16 @@ namespace ASCOM.Meade.net
}
}
if (_connectedDeviceIds.ContainsKey(driverId))
if (ConnectedDeviceIds.ContainsKey(driverId))
{
_connectedDeviceIds[driverId].Count--;
ConnectedDeviceIds[driverId].Count--;
}
}
}
public static bool IsConnected()
{
foreach (var device in _connectedDevices)
foreach (var device in ConnectedDevices)
{
if (device.Value.Count > 0)
return true;
@@ -351,8 +352,8 @@ namespace ASCOM.Meade.net
public static bool IsConnected(string deviceId)
{
if (_connectedDevices.ContainsKey(deviceId))
return (_connectedDevices[deviceId].Count > 0);
if (ConnectedDevices.ContainsKey(deviceId))
return (ConnectedDevices[deviceId].Count > 0);
else
return false;
}
@@ -381,13 +382,7 @@ namespace ASCOM.Meade.net
/// </summary>
public class DeviceHardware
{
private int _count;
internal int Count
{
set => _count = value;
get => _count;
}
internal int Count { set; get; }
internal DeviceHardware()
{
+6 -6
View File
@@ -10,20 +10,20 @@ namespace ASCOM.Meade.net
{
#region Autostar 497/Audiostar
public readonly static string Autostar497 = "Autostar";
public static readonly string Autostar497 = "Autostar";
//Autostar/Audiostar firmware revisions
public readonly static string Autostar497_30Ee = "30Ee";
public readonly static string Autostar497_31Ee = "31Ee";
public readonly static string Autostar497_43Eg = "43Eg";
public static readonly string Autostar497_30Ee = "30Ee";
public static readonly string Autostar497_31Ee = "31Ee";
public static readonly string Autostar497_43Eg = "43Eg";
#endregion
#region LX200GPS
public readonly static string LX200GPS = "LX2001";
public static readonly string LX200GPS = "LX2001";
public readonly static string LX200GPS_42G = "4.2G";
public static readonly string LX200GPS_42G = "4.2G";
#endregion
}
+7 -7
View File
@@ -77,11 +77,11 @@ namespace ASCOM.Meade.net
// Published in The Delphi Magazine 55, page 16
// Converted to C# by Kevin Gale
IntPtr foregroundWindow = GetForegroundWindow();
IntPtr Dummy = IntPtr.Zero;
IntPtr dummy = IntPtr.Zero;
uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, Dummy);
uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, dummy);
uint thisThreadId = GetWindowThreadProcessId(hWnd, Dummy);
uint thisThreadId = GetWindowThreadProcessId(hWnd, dummy);
if (AttachThreadInput(thisThreadId, foregroundThreadId, true))
{
@@ -94,12 +94,12 @@ namespace ASCOM.Meade.net
{
// Code by Daniel P. Stasinski
// Converted to C# by Kevin Gale
IntPtr Timeout = IntPtr.Zero;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
IntPtr timeout = IntPtr.Zero;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, dummy, SPIF_SENDCHANGE);
BringWindowToTop(hWnd); // IE 5.5 related hack
SetForegroundWindow(hWnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, timeout, SPIF_SENDCHANGE);
}
}
}
-2
View File
@@ -4,8 +4,6 @@ namespace ASCOM.Meade.net
{
public partial class FrmMain : Form
{
delegate void SetTextCallback(string text);
public FrmMain()
{
InitializeComponent();