Skip to content

Commit

Permalink
进一步修复了无法彻底退出的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zsh2401 committed Aug 13, 2020
1 parent dc9095e commit f70aa90
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ManagedAdb\CommandDriven\Unix\UnixCPM.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModuleInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MultipleDevices\DevicesCollection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\AdbServerKiller.cs" />
<Compile Include="..\AutumnBox.Basic.Shared\Util\LocalAdbServerKiller.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\ProcessExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\CommandResult.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Data\INotifyOutput.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected virtual void KillServer()
{
lock (concurrentLock)
{

using var cmd = new CommandProcedure()
{
FileName = AdbExecutableFile.ToString(),
Expand All @@ -109,9 +109,9 @@ protected virtual void KillServer()
};
cmd.InitializeAdbEnvironment(AdbClientDirectory, (ushort)ServerEndPoint.Port);

new AdbServerKiller(cmd).Kill().Wait();
new LocalAdbServerKiller(cmd).Kill().Wait();

SLogger.Info(this, "server killed");
SLogger.Info(this, "Adb Server has been stopped.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/AutumnBox.Basic.Shared/ModuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static class ModuleInfo
/// 指示版本
/// </summary>
public static Version Version => Version.Parse(VERSION_STR);
const string VERSION_STR = "2020.8.9";
const string VERSION_STR = "2020.8.14";
}
}
2 changes: 2 additions & 0 deletions src/AutumnBox.Basic.Shared/Util/AdbServerKiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public AdbServerKiller(ICommandProcedure killServerCommand)
{
this.commandKillServer = killServerCommand ?? throw new ArgumentNullException(nameof(killServerCommand));
}

public Task Kill()
{
return Task.Run(() =>
Expand All @@ -51,6 +52,7 @@ public Task Kill()
});

}

private void KilProcesses()
{
using var cmd = new CommandProcedure()
Expand Down
92 changes: 92 additions & 0 deletions src/AutumnBox.Basic.Shared/Util/LocalAdbServerKiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* ==============================================================================
*
* Filename: AdbServerKiller
* Description: 
*
* Version: 1.0
* Created: 2020/8/14 0:23:38
* Compiler: Visual Studio 2019
*
* Author: zsh2401
*
* ==============================================================================
*/
using AutumnBox.Basic.ManagedAdb.CommandDriven;
using AutumnBox.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AutumnBox.Basic.Util
{
internal sealed class LocalAdbServerKiller
{
private const int MAX_TIMEOUT = 3000;
private readonly ICommandProcedure commandKillServer;
private readonly ILogger logger;
public LocalAdbServerKiller(ICommandProcedure killServerCommand)
{
logger = LoggerFactory.Auto<LocalAdbServerKiller>();
commandKillServer = killServerCommand ?? throw new ArgumentNullException(nameof(killServerCommand));
}

public Task Kill()
{
return Task.Run(() =>
{
commandKillServer.OutputReceived += (s, e) =>
{
SLogger.Info(this, $"{e.Text}");
};
commandKillServer.ExecuteAsync();
Thread.Sleep(MAX_TIMEOUT);
if (commandKillServer.Status != CommandStatus.Executing && commandKillServer.Result.ExitCode == 0)
{
commandKillServer.Cancel();
commandKillServer.Dispose();
KilProcesses();
}
});
}

private void KilProcesses()
{
var psi = new ProcessStartInfo()
{
FileName = "taskkill",
Arguments = "/f /im adb.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false,
};
var proc = Process.Start(psi);
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.OutputDataReceived += (s, e) => SLogger<LocalAdbServerKiller>.Info(e.Data);
proc.ErrorDataReceived += (s, e) => SLogger<LocalAdbServerKiller>.Warn(e.Data);
//proc.WaitForExit();
Thread.Sleep(MAX_TIMEOUT);
if (proc.HasExited)
{
if (proc.ExitCode != 0)
{
logger.Warn("The command taskkil looks like not working. Exit code is " + proc.ExitCode);
}
logger.Info("The command taskkil has successfully executed.");
}
else
{
logger.Warn("The command taskkil looks like not working.");
}
proc.CancelOutputRead();
proc.CancelErrorRead();
proc.Dispose();
}
}
}
6 changes: 3 additions & 3 deletions src/AutumnBox.Core/AutumnBox.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<AssemblyName>AutumnBox.Core</AssemblyName>
<RootNamespace>AutumnBox.Core</RootNamespace>
<Nullable>enable</Nullable>
<Version>11.8</Version>
<Version>11.8.1</Version>
<Configurations>Debug;Release;SDK;Canary</Configurations>
<Authors>zsh2401</Authors>
<Company>zsh2401</Company>
<AssemblyVersion>11.8.0.0</AssemblyVersion>
<FileVersion>11.8.0.0</FileVersion>
<AssemblyVersion>11.8.1.0</AssemblyVersion>
<FileVersion>11.8.1.0</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Description>Let's Develop AutumnBox Extension!</Description>
Expand Down
8 changes: 4 additions & 4 deletions src/AutumnBox.GUI/AutumnBox.GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<AssemblyName>AutumnBox.GUI</AssemblyName>
<RootNamespace>AutumnBox.GUI</RootNamespace>
<ApplicationIcon>Resources\Icons\icon.ico</ApplicationIcon>
<Version>2020.8.9.2</Version>
<AssemblyVersion>2020.8.9.2</AssemblyVersion>
<Version>2020.8.14.0</Version>
<AssemblyVersion>2020.8.14.0</AssemblyVersion>
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
<Configurations>Debug;Release;Canary</Configurations>
<FileVersion>2020.8.9.2</FileVersion>
<FileVersion>2020.8.14.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;USE_SYS_JSON WIN32 NETCORE31 GREEN_RELEASE</DefineConstants>
<DefineConstants>TRACE;USE_SYS_JSON WIN32 NETCORE31 GREEN_RELEASE ENABLE_DEVICE_REPORTER</DefineConstants>
<OutputPath></OutputPath>
</PropertyGroup>

Expand Down
16 changes: 9 additions & 7 deletions src/AutumnBox.GUI/Services/Impl/AdbDevicesManagerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace AutumnBox.GUI.Services.Impl
[Component(Type = typeof(IAdbDevicesManager))]
sealed class AdbDevicesManagerImpl : IAdbDevicesManager, IDisposable
{
public IDevice SelectedDevice
public IDevice? SelectedDevice
{
get => _selectedDevice; set
{
Expand All @@ -31,15 +31,15 @@ public IDevice SelectedDevice
});
}
}
private IDevice _selectedDevice;
private IDevice? _selectedDevice;

public IEnumerable<IDevice> ConnectedDevices { get; private set; }
public IEnumerable<IDevice> ConnectedDevices { get; private set; } = new IDevice[0];

public event EventHandler DeviceSelectionChanged;
public event EventHandler? DeviceSelectionChanged;

private DevicesMonitor devicesMonitor = new DevicesMonitor();
private readonly DevicesMonitor devicesMonitor = new DevicesMonitor();

public event DevicesChangedHandler ConnectedDevicesChanged;
public event DevicesChangedHandler? ConnectedDevicesChanged;

public AdbDevicesManagerImpl()
{
Expand Down Expand Up @@ -84,7 +84,6 @@ void Dispose(bool disposing)
// TODO: 释放托管状态(托管对象)。
}
devicesMonitor.Cancel();
devicesMonitor = null;
// TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
// TODO: 将大型字段设置为 null。

Expand Down Expand Up @@ -118,8 +117,10 @@ private static void TrackDevicesInfo(IEnumerable<IDevice> devices)
{
{ "Count of Devices",devices.Count().ToString()},
};

Analytics.TrackEvent("Devices Changed", devicesChangedData);

#if ENABLE_DEVICE_REPORTER
Task.Run(() =>
{
Thread.CurrentThread.Name = "Device Info Reporter Thread";
Expand Down Expand Up @@ -150,6 +151,7 @@ private static void TrackDevicesInfo(IEnumerable<IDevice> devices)
}
}
});
#endif
}
}
}
2 changes: 1 addition & 1 deletion src/AutumnBox.GUI/Services/Impl/LoggingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public LoggingManager(IStorageManager storageManager)
}
public void Initialize()
{
var logger = new ConsoleLogger(false) + new AsyncFileLogger(OpenLogFileStream());
var logger = new TraceLogger() + new AsyncFileLogger(OpenLogFileStream());
Logging.Management.LoggingManager.Use(logger);
}
private FileStream OpenLogFileStream()
Expand Down
27 changes: 23 additions & 4 deletions src/AutumnBox.GUI/Services/Impl/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@ class WindowManager : LoggingObject, IWindowManager
{
public Window MainWindow => App.Current.MainWindow;

/// <summary>
/// 创建窗口
/// </summary>
/// <param name="windowName"></param>
/// <param name="args"></param>
/// <exception cref="Exception">窗口未找到</exception>
/// <returns></returns>
public Window CreateWindow(string windowName, params object[] args)
{
var type = Type.GetType("AutumnBox.GUI.Views.Windows." + windowName + "Window");
var window = (Window)Activator.CreateInstance(type, args);
window.Owner = App.Current.MainWindow;
return window;
if (Type.GetType("AutumnBox.GUI.Views.Windows." + windowName + "Window") is Type type
&&
Activator.CreateInstance(type, args) is Window win)
{
App.Current.MainWindow.Closed += (s, e) =>
{
try {
win.Close();
} catch { }
};
return win;
}
else
{
throw new Exception("Window not found");
}
}

public void Show(string windowName)
Expand Down
27 changes: 18 additions & 9 deletions src/AutumnBox.GUI/Views/Windows/LogEasyWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AutumnBox.Logging.Management;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
Expand All @@ -21,27 +22,35 @@ namespace AutumnBox.GUI.Views.Windows
/// </summary>
public partial class LogEasyWindow : Window
{
private readonly ILoggingManager logMg;
public LogEasyWindow()
{
InitializeComponent();
var logMg = this.GetComponent<ILoggingManager>();
logMg = this.GetComponent<ILoggingManager>();
logMg.Logs.All((log) =>
{
logBox.AppendText(log.ToFormatedString() + "\n");
return true;
});
logBox.ScrollToEnd();
logMg.Logs.CollectionChanged += (s, e) =>
logMg.Logs.CollectionChanged += Logs_CollectionChanged;
}

private void Logs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
foreach (ILog? item in e.NewItems)
{
foreach (ILog item in e.NewItems)
{
this.Dispatcher.Invoke(() => this.logBox.AppendText(item.ToFormatedString() + "\n"));
}
App.Current.Dispatcher.Invoke(() => logBox.ScrollToEnd());
this.Dispatcher.Invoke(() => this.logBox.AppendText(item?.ToFormatedString() + "\n"));
}
};
App.Current.Dispatcher.Invoke(() => logBox.ScrollToEnd());
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
logMg.Logs.CollectionChanged -= Logs_CollectionChanged;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Management\ConsoleLogger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Management\CoreLoggerBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Management\ICoreLogger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Management\TraceLogger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Management\WriteOnLastFileLogger.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModuleInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SLogger.cs" />
Expand Down
32 changes: 32 additions & 0 deletions src/AutumnBox.Logging.Shared/Management/TraceLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* ==============================================================================
*
* Filename: TraceLogger
* Description: 
*
* Version: 1.0
* Created: 2020/8/14 1:44:55
* Compiler: Visual Studio 2019
*
* Author: zsh2401
*
* ==============================================================================
*/
using System.Diagnostics;

namespace AutumnBox.Logging.Management
{
public class TraceLogger : CoreLoggerBase
{
public void Dispose()
{
//pass
}

public override void Log(ILog log)
{
Trace.WriteLine(log.ToFormatedString());
}
}
}

0 comments on commit f70aa90

Please sign in to comment.