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 inruntime.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、設定自定義依賴項屬性
publicAxisDebug()
{
InitializeComponent();
Binding binding = newBinding()
{
Source = this.DataContext,
Path = newPropertyPath("Model.Id"),
Mode = BindingMode.TwoWay
};
BindingOperations.SetBinding(this, AxisDebug.IdProperty, binding);
}
#region依賴項屬性
publicstaticreadonlyDependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(AxisDebug));
#endregion
publicintId
{
get{ return(int)GetValue(IdProperty); }
set{ SetValue(IdProperty, value); }
}