-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ready for Review - [Mouse Without Borders] - refactoring "Common" classes (Part 4) - #35155 #37579
Open
mikeclayton
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
mikeclayton:dev/mikeclayton/mwb-common-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,474
−1,450
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c31e671
[MouseWithoutBorders] - moving Common.Event.cs -> Core\Event.cs - #35155
mikeclayton 4885c30
[MouseWithoutBorders] - moving Common.Service.cs -> Core\Service.cs -…
mikeclayton e4288d0
[MouseWithoutBorders] - moving Common.Launch.cs -> Core\Launch.cs - #…
mikeclayton f208794
[MouseWithoutBorders] - moving Common.Helper.cs -> Core\Helper.cs - #…
mikeclayton dd7c7d2
[MouseWithoutBorders] - refactoring and fixes for logger unit test - …
mikeclayton 1077d84
[MouseWithoutBorders] - cleaning up changes - #35155
mikeclayton e9cbb60
[MouseWithoutBorders] - re-[Ignore]-ing test - #35155
mikeclayton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[MouseWithoutBorders] - moving Common.Service.cs -> Core\Service.cs - #…
- Loading branch information
commit 4885c30103f1ac80968f8c968b190125607446da
There are no files selected for viewing
161 changes: 0 additions & 161 deletions
161
src/modules/MouseWithoutBorders/App/Class/Common.Service.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// 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.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.ServiceProcess; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
// <summary> | ||
// Service control code. | ||
// </summary> | ||
// <history> | ||
// 2008 created by Truong Do (ductdo). | ||
// 2009-... modified by Truong Do (TruongDo). | ||
// 2023- Included in PowerToys. | ||
// </history> | ||
using MouseWithoutBorders.Class; | ||
|
||
[module: SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Scope = "member", Target = "MouseWithoutBorders.Common.#StartMouseWithoutBordersService()", Justification = "Dotnet port with style preservation")] | ||
|
||
namespace MouseWithoutBorders.Core; | ||
|
||
internal static class Service | ||
{ | ||
private static bool shownErrMessage; | ||
private static DateTime lastStartServiceTime = DateTime.UtcNow; | ||
|
||
internal static void StartMouseWithoutBordersService(string desktopToRunMouseWithoutBordersOn = null, string startTag1 = "byapp", string startTag2 = null) | ||
{ | ||
// NOTE(@yuyoyuppe): the new flow assumes we run both mwb processes directly from the svc. | ||
if (Common.RunWithNoAdminRight || true) | ||
{ | ||
return; | ||
} | ||
|
||
Logger.Log($"{nameof(StartMouseWithoutBordersService)}: {Logger.GetStackTrace(new StackTrace())}."); | ||
|
||
Task task = Task.Run(() => | ||
{ | ||
Process[] ps = Process.GetProcessesByName("MouseWithoutBordersSvc"); | ||
|
||
if (ps.Length != 0) | ||
{ | ||
if (DateTime.UtcNow - lastStartServiceTime < TimeSpan.FromSeconds(5)) | ||
{ | ||
Logger.Log($"{nameof(StartMouseWithoutBordersService)}: Aborted."); | ||
return; | ||
} | ||
|
||
foreach (Process pp in ps) | ||
{ | ||
Logger.Log(string.Format(CultureInfo.InvariantCulture, "Killing process MouseWithoutBordersSvc {0}.", pp.Id)); | ||
pp.KillProcess(); | ||
} | ||
} | ||
|
||
lastStartServiceTime = DateTime.UtcNow; | ||
ServiceController service = new("MouseWithoutBordersSvc"); | ||
|
||
try | ||
{ | ||
Logger.Log("Starting " + service.ServiceName); | ||
} | ||
catch (Exception) | ||
{ | ||
if (!shownErrMessage) | ||
{ | ||
shownErrMessage = true; | ||
_ = MessageBox.Show( | ||
Application.ProductName + " is not installed yet, please run Setup.exe first!", | ||
Application.ProductName, | ||
MessageBoxButtons.OK, | ||
MessageBoxIcon.Error); | ||
} | ||
|
||
return; | ||
} | ||
|
||
try | ||
{ | ||
int c = 0; | ||
|
||
while (service.Status != ServiceControllerStatus.Stopped && c++ < 5) | ||
{ | ||
Thread.Sleep(1000); | ||
service = new ServiceController("MouseWithoutBordersSvc"); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(desktopToRunMouseWithoutBordersOn)) | ||
{ | ||
startTag2 ??= Process.GetCurrentProcess().SessionId.ToString(CultureInfo.InvariantCulture); | ||
service.Start(new string[] { startTag1, startTag2 }); | ||
} | ||
else | ||
{ | ||
service.Start(new string[] { desktopToRunMouseWithoutBordersOn }); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.Log(e); | ||
|
||
// ERROR_SERVICE_ALREADY_RUNNING | ||
if (!(shownErrMessage || ((e?.InnerException as Win32Exception)?.NativeErrorCode == 1056))) | ||
{ | ||
shownErrMessage = true; | ||
_ = MessageBox.Show( | ||
"Cannot start service " + service.ServiceName + ": " + e.Message, | ||
Common.BinaryName, | ||
MessageBoxButtons.OK, | ||
MessageBoxIcon.Error); | ||
} | ||
|
||
return; | ||
} | ||
}); | ||
|
||
// Wait for the task while not blocking the UI thread. | ||
do | ||
{ | ||
Common.MMSleep(1); | ||
|
||
if (task.IsCanceled || task.IsCompleted || task.IsFaulted) | ||
{ | ||
break; | ||
} | ||
} | ||
while (true); | ||
} | ||
|
||
internal static void StartServiceAndSendLogoffSignal() | ||
{ | ||
try | ||
{ | ||
Process[] p = Process.GetProcessesByName("winlogon"); | ||
Process me = Process.GetCurrentProcess(); | ||
string myWinlogon = p?.FirstOrDefault(item => item.SessionId == me.SessionId)?.Id.ToString(CultureInfo.InvariantCulture) ?? null; | ||
|
||
if (string.IsNullOrWhiteSpace(myWinlogon)) | ||
{ | ||
StartMouseWithoutBordersService(null, "logoff"); | ||
} | ||
else | ||
{ | ||
StartMouseWithoutBordersService(null, "logoff", myWinlogon); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.Log($"{nameof(StartServiceAndSendLogoffSignal)}: {e.Message}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of '|| true' in the conditional always evaluates to true, bypassing the intended service start logic. Consider removing '|| true' or revising the condition to properly control service startup.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Common.RunWithNoAdminRight || true)
was in the originalCommon.Service.cs
file - Copilot's detected it as it thinksService.cs
is a new file rather than a rename and is treating this as a "new" issue. I'd suggest leaving as-is for this PR.