Skip to content

Commit

Permalink
Add exit button
Browse files Browse the repository at this point in the history
Small cleanup
  • Loading branch information
Virenbar committed Jun 5, 2021
1 parent d2a83be commit 9b13b1b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 107 deletions.
161 changes: 89 additions & 72 deletions FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion FormMain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WMRControl
Expand Down Expand Up @@ -91,6 +90,11 @@ private void FormMain_Load(object sender, EventArgs e)
WMRM.Init();
}

private void B_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}

#endregion Form Events

#region Tray Events
Expand Down
3 changes: 3 additions & 0 deletions FormMain.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,9 @@
AAD//wAAwAMAAIABAAAcOQAAH/kAABw5AAAcOQAAAAEAAIADAAD//wAA//8AAP//AAD//wAA
</value>
</data>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAYAAAAAAAEAIACMDwAAZgAAAICAAAABACAAKAgBAPIPAABAQAAAAQAgAChCAAAaGAEAMDAAAAEA
Expand Down
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ internal static class Program
[STAThread]
private static void Main(string[] args)
{
if (args.Length > 0 && new[] { "-enable", "-disable" }.Contains(args[0]))
if (args.Length > 0 && new[] { "-enable", "-disable" }.Contains(args[0].ToLower()))
{
var WMRM = new WMRManager();
WMRM.Init();
WMRM.SetWMRState(args[0] == "-enable");
WMRM.SetWMRState(args[0].ToLower() == "-enable");
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions WMRControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<Version>1.0.1</Version>
<Authors>Virenbar</Authors>
<Configurations>Debug;Release;DebigArg</Configurations>
<PackageLicenseExpression></PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
50 changes: 18 additions & 32 deletions WMRManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,16 @@ internal class WMRManager
{
private const string Query = @"SELECT * FROM Win32_PnPEntity WHERE PNPClass LIKE 'Holographic'";
private const string Scope = "root\\CIMV2";
private ManagementObject WMRObject;
private bool IsInvoking;
private ManagementObject WMRObject;

#region Properties
public bool IsEnabled { get; set; }
public string Name => WMRObject.Properties["Name"].Value.ToString();
public string State => ParseError((uint)WMRObject.Properties["ConfigManagerErrorCode"].Value);
public string State => ParseError(ErrorCode);
private uint ErrorCode => (uint)WMRObject.Properties["ConfigManagerErrorCode"].Value;
#endregion Properties

public void DisableWMR()
{
_ = InvokeMethod("Disable");
}

public void EnableWMR()
{
_ = InvokeMethod("Enable");
}

public void SetWMRState(bool state)
{
_ = InvokeMethod(state ? "Enable" : "Disable");
}

public void Init()
{
using (var MOS = new ManagementObjectSearcher(Scope, Query))
Expand All @@ -41,39 +27,39 @@ public void Init()
{
if (Objects.Count == 0) { throw new Exception("WMR Device not found"); }
WMRObject = Objects.Cast<ManagementObject>().First();
IsEnabled = (uint)WMRObject.Properties["ConfigManagerErrorCode"].Value != 22;
IsEnabled = ErrorCode != 22;
}
}
OnStateChanged();
}

public void SetWMRState(bool state)
{
_ = InvokeMethod(state ? "Enable" : "Disable");
}

protected virtual void OnStateChanged()
{
EventHandler handler = StateChanged;
handler?.Invoke(this, EventArgs.Empty);
}

private async Task InvokeMethod(string method)
{
var MOO = new ManagementOperationObserver();
MOO.Completed += new CompletedEventHandler(MOO_Completed);
IsInvoking = true;
var MOO = new ManagementOperationObserver();
MOO.Completed += (sender, e) => { IsInvoking = false; };
WMRObject.InvokeMethod(MOO, method, null);
while (IsInvoking)
{
await Task.Delay(500);
}
await Task.Delay(1000);
WMRObject.Get();
IsEnabled = (uint)WMRObject.Properties["ConfigManagerErrorCode"].Value != 22;
IsEnabled = ErrorCode != 22;
OnStateChanged();
}

private void MOO_Completed(object sender, CompletedEventArgs e)
{
IsInvoking = false;
}

protected virtual void OnStateChanged()
{
EventHandler handler = StateChanged;
handler?.Invoke(this, EventArgs.Empty);
}

private string ParseError(uint error)
{
switch (error)
Expand Down

0 comments on commit 9b13b1b

Please sign in to comment.