Commit bd1a1995 by liulongfei

添加转化器,计时器支持

parent 9e16edda
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
namespace VIZ.Framework.Core
{
/// <summary>
/// Bool --> ImageSource 转化器
/// </summary>
public class Bool2ImageSourceConverter : IValueConverter
{
/// <summary>
/// 当值为True时的返回值
/// </summary>
public ImageSource TrueResult { get; set; }
/// <summary>
/// 当值为False时的返回值
/// </summary>
public ImageSource FalseResult { get; set; }
/// <summary>
/// 当值为Null时的返回值
/// </summary>
public ImageSource NullResult { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool?)
{
bool? b = (bool?)value;
if (b == null)
return this.NullResult;
return b.Value ? this.TrueResult : this.FalseResult;
}
else if (value is bool)
{
bool b = (bool)value;
return b ? this.TrueResult : this.FalseResult;
}
else
{
return this.NullResult;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace VIZ.Framework.Core
{
/// <summary>
/// 非空字符串到Visibility转化
/// </summary>
public class StringNotNull2VisibilityConverter : IValueConverter
{
/// <summary>
/// 为空时的返回值
/// </summary>
public Visibility NullResult { get; set; } = Visibility.Hidden;
/// <summary>
/// 非空时的返回值
/// </summary>
public Visibility NotNullResult { get; set; } = Visibility.Visible;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is string))
return this.NullResult;
string str = value as string;
return string.IsNullOrWhiteSpace(str) ? this.NullResult : this.NotNullResult;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 计时器管理器
/// </summary>
public interface ITimerManager : IDisposable
{
/// <summary>
/// 注册
/// </summary>
/// <param name="key">键</param>
/// <param name="interval">触发间隔</param>
/// <param name="action">行为</param>
/// <returns>计时器</returns>
TimerInfo Register(string key, double interval, Action action);
/// <summary>
/// 注册
/// </summary>
/// <param name="key">键</param>
/// <param name="interval">触发间隔</param>
/// <param name="action">行为</param>
/// <returns>计时器</returns>
TimerInfo Register(string key, TimeSpan interval, Action action);
/// <summary>
/// 注销
/// </summary>
/// <param name="key">键</param>
/// <returns>计时器信息</returns>
TimerInfo UnRegister(string key);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 计时器信息
/// </summary>
public class TimerInfo : IDisposable
{
/// <summary>
/// 键
/// </summary>
public string Key { get; set; }
/// <summary>
/// 执行时间
/// </summary>
public DateTime ExecuteTime { get; set; }
/// <summary>
/// 间隔
/// </summary>
public TimeSpan Interval { get; set; }
/// <summary>
/// 执行次数
/// </summary>
public long ExecuteCount { get; set; }
/// <summary>
/// 行为
/// </summary>
public Action Action { get; set; }
/// <summary>
/// 是否被销毁
/// </summary>
internal bool IsDisposed;
/// <summary>
/// 销毁
/// </summary>
public void Dispose()
{
this.IsDisposed = true;
this.Action = null;
}
}
}
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 计时器管理器
/// </summary>
public class TimerManager : ITimerManager
{
/// <summary>
/// 日志
/// </summary>
private static ILog log = LogManager.GetLogger(typeof(TimerManager));
/// <summary>
/// 缓存池
/// </summary>
private Dictionary<string, TimerInfo> Pool = new Dictionary<string, TimerInfo>(100);
/// <summary>
/// 延时线程
/// </summary>
private System.Threading.Thread Thread;
/// <summary>
/// 是否已经被释放
/// </summary>
private bool IsDisposabled = false;
public TimerManager()
{
this.Thread = new System.Threading.Thread(this.Execute);
this.Thread.Start();
}
/// <summary>
/// 注册
/// </summary>
/// <param name="key">键</param>
/// <param name="interval">触发间隔</param>
/// <param name="action">行为</param>
/// <returns>计时器</returns>
public TimerInfo Register(string key, double interval, Action action)
{
return this.Register(key, TimeSpan.FromSeconds(interval), action);
}
/// <summary>
/// 注册
/// </summary>
/// <param name="key">键</param>
/// <param name="interval">触发间隔</param>
/// <param name="action">行为</param>
/// <returns>计时器</returns>
public TimerInfo Register(string key, TimeSpan interval, Action action)
{
lock (this.Pool)
{
if (this.Pool.TryGetValue(key, out TimerInfo info))
{
info.Action = action;
return info;
}
info = new TimerInfo();
info.Key = key;
info.Action = action;
info.Interval = interval;
info.Action = action;
this.Pool.Add(key, info);
return info;
}
}
/// <summary>
/// 注销
/// </summary>
/// <param name="key">键</param>
/// <returns>计时器信息</returns>
public TimerInfo UnRegister(string key)
{
lock (this.Pool)
{
if (!this.Pool.TryGetValue(key, out TimerInfo info))
return null;
this.Pool.Remove(key);
info.Dispose();
return info;
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
try
{
lock (this.Pool)
{
foreach (TimerInfo info in this.Pool.Values)
{
info.Dispose();
}
this.Pool.Clear();
}
this.IsDisposabled = true;
this.Thread = null;
}
catch (Exception ex)
{
log.Error(ex);
}
}
/// <summary>
/// 执行延时任务
/// </summary>
private void Execute()
{
while (!this.IsDisposabled)
{
lock (this.Pool)
{
DateTime now = DateTime.Now;
List<TimerInfo> removeList = new List<TimerInfo>();
foreach (TimerInfo info in this.Pool.Values)
{
if (this.IsDisposabled)
break;
if (info.ExecuteTime + info.Interval > now)
continue;
try
{
if (info.IsDisposed)
{
removeList.Add(info);
}
info.Action?.Invoke();
}
catch (Exception ex)
{
log.Error(ex);
}
finally
{
info.ExecuteCount++;
info.ExecuteTime = now;
}
}
foreach (var item in removeList)
{
this.Pool.Remove(item.Key);
}
}
System.Threading.Thread.Sleep(200);
}
}
}
}
\ No newline at end of file
......@@ -93,6 +93,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\Converter\Bool2ImageSourceConverter.cs" />
<Compile Include="Core\Converter\Bool2BoolConverterSimple.cs" />
<Compile Include="Core\Converter\Bool2BoolConverter.cs" />
<Compile Include="Core\Converter\Bool2StringConverter.cs" />
......@@ -103,6 +104,7 @@
<Compile Include="Core\Converter\String2ImageSourceConverter.cs" />
<Compile Include="Core\Converter\StringAppendConverter.cs" />
<Compile Include="Core\Converter\NumberAddConverter.cs" />
<Compile Include="Core\Converter\StringNotNull2VisibilityConverter.cs" />
<Compile Include="Core\Converter\StringNotNull2BoolConverter.cs" />
<Compile Include="Core\Enum\EnumHelper.cs" />
<Compile Include="Core\Enum\EnumModel.cs" />
......@@ -140,6 +142,9 @@
<Compile Include="Core\Message\MessageInfo.cs" />
<Compile Include="Core\Message\MessageManager.cs" />
<Compile Include="Core\Task\TaskInfo.cs" />
<Compile Include="Core\Timer\ITimerManager.cs" />
<Compile Include="Core\Timer\TimerInfo.cs" />
<Compile Include="Core\Timer\TimerManager.cs" />
<Compile Include="Core\VIZ\ControlObjects\IVizControlObjctsValue.cs" />
<Compile Include="Core\VIZ\ControlObjects\VizControlObjctsCommand.cs" />
<Compile Include="Core\VIZ\ControlObjects\VizControlObjectsElementValue.cs" />
......
......@@ -36,6 +36,11 @@ namespace VIZ.Framework.Domain
public static IDelayManager DelayManager { get; private set; } = new DelayManager();
/// <summary>
/// 计时器管理器
/// </summary>
public static ITimerManager TimerManager { get; private set; } = new TimerManager();
/// <summary>
/// 服务管理器
/// </summary>
public static IServiceManager ServiceManager { get; private set; } = new ServiceManager();
......
......@@ -64,13 +64,16 @@ namespace VIZ.Framework.Module
// Step 5. 初始化延时
appSetups.Add(new AppSetup_DelayInit());
// Step 6. 初始化循环
// Step 6. 初始化计时器
appSetups.Add(new AppSetup_TimerInit());
// Step 7. 初始化循环
appSetups.Add(new AppSetup_Loop());
// Step 7. 初始化对象池
// Step 8. 初始化对象池
appSetups.Add(new AppSetup_ObjectPool());
// Step 8. 初始化辅助类
// Step 9. 初始化辅助类
appSetups.Add(new AppSetup_Helper());
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using VIZ.Framework.Core;
using VIZ.Framework.Domain;
using VIZ.Framework.Connection;
using VIZ.Framework.Storage;
namespace VIZ.Framework.Module
{
/// <summary>
/// 应用程序启动 -- 初始化计时器
/// </summary>
public class AppSetup_TimerInit : AppSetupBase
{
/// <summary>
/// 日志
/// </summary>
private static ILog log = LogManager.GetLogger(typeof(AppSetup_TimerInit));
/// <summary>
/// 描述
/// </summary>
public override string Detail { get; } = "应用程序启动 -- 初始化计时器";
/// <summary>
/// 执行启动
/// </summary>
/// <param name="context">应用程序启动上下文</param>
/// <returns>是否成功执行</returns>
public override bool Setup(AppSetupContext context)
{
return true;
}
/// <summary>
/// 执行关闭
/// </summary>
/// <param name="context">应用程序启动上下文</param>
public override void Shutdown(AppSetupContext context)
{
ApplicationDomain.TimerManager?.Dispose();
}
}
}
......@@ -101,6 +101,7 @@
<Compile Include="Setup\AppSetupContext.cs" />
<Compile Include="Setup\IAppSetup.cs" />
<Compile Include="Setup\Message\AppShutDownMessage.cs" />
<Compile Include="Setup\Provider\Setup\AppSetup_TimerInit.cs" />
<Compile Include="Setup\Provider\Setup\AppSetup_ObjectPool.cs" />
<Compile Include="Setup\Provider\Setup\AppSetup_ApplicationSafe.cs" />
<Compile Include="Setup\Provider\Setup\AppSetup_Helper.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment