mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
Cleanup the code
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
using System;
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Espresso.Shell.Core
|
namespace Espresso.Shell.Core
|
||||||
|
|||||||
42
src/modules/espresso/Espresso.Shell/Core/SettingsHelper.cs
Normal file
42
src/modules/espresso/Espresso.Shell/Core/SettingsHelper.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Espresso.Shell.Core
|
||||||
|
{
|
||||||
|
public class SettingsHelper
|
||||||
|
{
|
||||||
|
const int ERROR_SHARING_VIOLATION = 32;
|
||||||
|
const int ERROR_LOCK_VIOLATION = 33;
|
||||||
|
|
||||||
|
public static FileStream GetSettingsFile(string path, int retries)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < retries; i++)
|
||||||
|
{
|
||||||
|
FileStream fileStream = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||||
|
return fileStream;
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
var errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
|
||||||
|
if (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION)
|
||||||
|
{
|
||||||
|
Console.WriteLine("There was another process using the file, so couldn't pick the settings up.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(50);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
using Newtonsoft.Json;
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Espresso.Shell.Models
|
namespace Espresso.Shell.Models
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
using Espresso.Shell.Core;
|
// Copyright (c) Microsoft Corporation
|
||||||
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using Espresso.Shell.Core;
|
||||||
using Espresso.Shell.Models;
|
using Espresso.Shell.Models;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.CommandLine;
|
using System.CommandLine;
|
||||||
using System.CommandLine.Invocation;
|
using System.CommandLine.Invocation;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Espresso.Shell
|
namespace Espresso.Shell
|
||||||
@@ -15,13 +18,12 @@ namespace Espresso.Shell
|
|||||||
private static Mutex mutex = null;
|
private static Mutex mutex = null;
|
||||||
private const string appName = "Espresso";
|
private const string appName = "Espresso";
|
||||||
|
|
||||||
const int ERROR_SHARING_VIOLATION = 32;
|
public static Mutex Mutex { get => mutex; set => mutex = value; }
|
||||||
const int ERROR_LOCK_VIOLATION = 33;
|
|
||||||
|
|
||||||
static int Main(string[] args)
|
static int Main(string[] args)
|
||||||
{
|
{
|
||||||
bool instantiated;
|
bool instantiated;
|
||||||
mutex = new Mutex(true, appName, out instantiated);
|
Mutex = new Mutex(true, appName, out instantiated);
|
||||||
|
|
||||||
if (!instantiated)
|
if (!instantiated)
|
||||||
{
|
{
|
||||||
@@ -170,7 +172,7 @@ namespace Espresso.Shell
|
|||||||
{
|
{
|
||||||
EspressoSettingsModel settings = null;
|
EspressoSettingsModel settings = null;
|
||||||
|
|
||||||
var fileStream = GetSettingsFile(fullPath, 10);
|
var fileStream = SettingsHelper.GetSettingsFile(fullPath, 10);
|
||||||
if (fileStream != null)
|
if (fileStream != null)
|
||||||
{
|
{
|
||||||
using (fileStream)
|
using (fileStream)
|
||||||
@@ -240,35 +242,9 @@ namespace Espresso.Shell
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"There was a problem reading the configuration file.\n{ex.Message}");
|
Console.WriteLine($"There was a problem reading the configuration file.\n{ex.Message}");
|
||||||
//ForceExit($"There was a problem reading the configuration file.\n{ex.Message}", 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FileStream GetSettingsFile(string path, int retries)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < retries; i++)
|
|
||||||
{
|
|
||||||
FileStream fileStream = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
|
|
||||||
return fileStream;
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
var errorCode = Marshal.GetHRForException(ex) & ((1 << 16) - 1);
|
|
||||||
if (errorCode == ERROR_SHARING_VIOLATION || errorCode == ERROR_LOCK_VIOLATION)
|
|
||||||
{
|
|
||||||
Console.WriteLine("There was another process using the file, so couldn't pick the settings up.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread.Sleep(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ResetNormalPowerState()
|
private static void ResetNormalPowerState()
|
||||||
{
|
{
|
||||||
bool success = APIHelper.SetNormalKeepAwake();
|
bool success = APIHelper.SetNormalKeepAwake();
|
||||||
|
|||||||
Reference in New Issue
Block a user