C#学习细节 (三)托管线程ID\跨线程操作动态集合SourceCollection\Nuget包版本问题\目标框架版本问题\自定义依赖项属性
1、托管线程ID
(1)获取当前运行的托管线程ID
Console.WriteLine("当前线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
(2)获取程序所有的托管线程
c# - 获取线程列表 - 堆栈溢出 (stackoverflow.com)
(1)需要Nuget包,Microsoft.Diagnostics.Runtime
using Microsoft.Diagnostics.Runtime;
using (DataTarget target = DataTarget.AttachToProcess(System.Diagnostics.Process.GetCurrentProcess().Id, 5000, AttachFlag.Passive))
{
ClrRuntime runtime = target.ClrVersions.First().CreateRuntime();
foreach (ClrThread thread in runtime.Threads)
{
Console.WriteLine("所有线程:" + thread.ManagedThreadId);
}
}
2、跨线程操作动态集合SourceCollection,委托主线程操作(可能是ads的通知方法是新创建的线程)
System.Windows.Application.Current.Dispatcher.Invoke((Action)(delegate
{
//代码
model.CurrentInfo.Add(currentInfo);
Console.WriteLine("主线程:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
}));
3、一个解决方案下的所有项目安装的Nuget包都在解决方案目录下的packages中
(1)如果不同项目引用的相同的包,则项目的目标框架(.net4.0/.net4.5等)和包的版本必须一致,
如果项目更换了目标框架,则必须重新安装对应版本的包。
(2)手动引用dll,不使用包管理器
4、高版本的目标框架程序可引用低版本目标框架程序
例如:.net4.5的程序能引动.net4.0的dll,但net4.0不能引用net4.5的dll
5、设置自定义依赖项属性
public AxisDebug()
{
InitializeComponent();
Binding binding = new Binding()
{
Source = this.DataContext,
Path = new PropertyPath("Model.Id"),
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(this, AxisDebug.IdProperty, binding);
}
#region 依赖项属性
public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(AxisDebug));
#endregion
public int Id
{
get { return (int)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}

浙公网安备 33010602011771号