Skip to content

Commit

Permalink
perf: 优化 dll 更新时,多次重载的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
uyoufu committed Feb 23, 2024
1 parent b37f282 commit 5d10db7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
6 changes: 6 additions & 0 deletions MSAddinTest/Core/FuncResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ public FuncResult(bool ok,string message):this(ok)
/// 数据
/// </summary>
public object Data { get; set; }

// 与 bool 类型的隐式转换
public static implicit operator bool(FuncResult result)
{
return result.Ok;
}
}
}
34 changes: 27 additions & 7 deletions MSAddinTest/Core/Loader/AutoReloader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Bentley.MstnPlatformNET;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;

namespace MSAddinTest.Core.Loader
Expand All @@ -16,6 +18,11 @@ internal class AutoReloader
{
private readonly FileSystemWatcher _watcher;
private readonly PluginAssemblyLoader _assemblyLoader;
private readonly Timer _reloadTimer = new Timer(1000)
{
Enabled = false,
AutoReset = true
};

public AutoReloader(PluginAssemblyLoader assemblyLoader)
{
Expand All @@ -27,6 +34,7 @@ public AutoReloader(PluginAssemblyLoader assemblyLoader)
NotifyFilter = NotifyFilters.LastWrite
};
_watcher.Changed += Watcher_Changed;
_reloadTimer.Elapsed += ReloadTimer_Elapsed;

// 从设置中获取是否自动重载
var setting = _assemblyLoader.Setup.PluginSetting;
Expand All @@ -35,26 +43,38 @@ public AutoReloader(PluginAssemblyLoader assemblyLoader)
// 监听数据变化
setting.SettingChanged += (arg) =>
{
if(arg.FieldName == "autoReload" && bool.TryParse(arg.Value,out bool result))
if (arg.FieldName == "autoReload" && bool.TryParse(arg.Value, out bool result))
{
_watcher.EnableRaisingEvents = result;
}
};
}

}

private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
// 判断是否是当前文件
if (e.FullPath != _assemblyLoader.Setup.DllFullPath) return;
{
// 只有当前文件才触发
if (e.FullPath != _assemblyLoader.Setup.DllFullPath)
{
return;
}

// 多次触发时,重置计时器
_reloadTimer.Stop();
_reloadTimer.Start();
}

private void ReloadTimer_Elapsed(object sender, ElapsedEventArgs e)
{
_reloadTimer.Stop();
// 事件是在另一个线程触发的,直接重载会报错
// 切换到主线程执行
Application.Current.Dispatcher.Invoke(() =>
{
// 文件更改后,需要重新加载
_assemblyLoader.Reload();
});

});
}
}
}
11 changes: 6 additions & 5 deletions MSAddinTest/Core/Loader/PluginAssemblyLoader_Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ private Assembly AppDomain_TypeResolve(object sender, ResolveEventArgs args)

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
// 获取对应版本的程序集
// 获取最新版本的程序集
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly targetAssembly = assemblies.LastOrDefault(x => x.FullName.Contains(args.Name));
string assemblyName = args.Name.Split(',')[0].Trim();
Assembly targetAssembly = assemblies.LastOrDefault(x => x.FullName.Contains(assemblyName));

if (targetAssembly != null) return targetAssembly;

// 找到文件,然后加载
string targetFileName = args.Name.Split(',')[0];
string fileName = _allFileNames.Find(x => x.Contains(targetFileName));
// 找到文件,然后加载
string assemblyFileName = $"{assemblyName}.dll";
string fileName = _allFileNames.Find(x => x.EndsWith(assemblyFileName));
if (fileName == null) return null;

byte[] bytes = File.ReadAllBytes(fileName);
Expand Down
10 changes: 8 additions & 2 deletions MSAddinTest/Core/Loader/PluginAssemblyLoader_Methods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MSAddinTest.MSTestInterface;
using Bentley.MstnPlatformNET;
using MSAddinTest.MSTestInterface;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -45,7 +46,12 @@ public FuncResult Execute(string nameWithParams)
/// </summary>
public void Reload()
{
LoadAssembly();
var reloadResult = LoadAssembly();
if (reloadResult)
{
// 提示成功
MessageCenter.Instance.ShowInfoMessage($"{Setup.PluginName} 插件重载成功!", "", false);
}
}
}
}

0 comments on commit 5d10db7

Please sign in to comment.