答案:
Application.Current.Dispatcher.Invoke((Action)(() => { logs.Add(this); }));我尝试为我的服务器创建一个日志系统(Log.cs)
我做了这个类日志:
public class Log { public enum Flags { [Description("SERVER - OK")] OK, [Description("SERVER - ERROR")] ERROR } public string EtatLog { get; set; } public string MessageLog { get; set; } public static ObservableCollection<Log> logs = new ObservableCollection<Log>(); public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; logs.Add(this); } private string GetEnumDescription(Enum enumValue) { var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString(); } }我已经在我的Datagrid上绑定了我的ObservableCollection,就像那样:
<DataGrid x:Name="GridLogs" Margin="0,5,0,0" ItemsSource="{Binding}" AutoGenerateColumns="False" x:FieldModifier="public"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding EtatLog}" Header="ID du Logs"/> <DataGridTextColumn Binding="{Binding MessageLog}" Header="Message du Logs" Width="*"/> </DataGrid.Columns> </DataGrid>现在这是我的问题:我想创建一个服务器,所以我使用很多线程来运行它(比如我的Login Listener)。 如果我有一个新的连接(用户)我想在我的列表中写一个日志,所以我这样做:Log log = new Log(Log.Flags.OK,“New co ...”);
但是,我使用多线程,所以我的数据网格无法更新,因为它在主线程上运行...
我有这个错误(法语):
我该如何解决我的问题?
ANSWER :
Application.Current.Dispatcher.Invoke((Action)(() => { logs.Add(this); }));I try to create a log system for my server (Log.cs)
I made this class log:
public class Log { public enum Flags { [Description("SERVER - OK")] OK, [Description("SERVER - ERROR")] ERROR } public string EtatLog { get; set; } public string MessageLog { get; set; } public static ObservableCollection<Log> logs = new ObservableCollection<Log>(); public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; logs.Add(this); } private string GetEnumDescription(Enum enumValue) { var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString(); } }I have binded my ObservableCollection on my Datagrid like that :
<DataGrid x:Name="GridLogs" Margin="0,5,0,0" ItemsSource="{Binding}" AutoGenerateColumns="False" x:FieldModifier="public"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding EtatLog}" Header="ID du Logs"/> <DataGridTextColumn Binding="{Binding MessageLog}" Header="Message du Logs" Width="*"/> </DataGrid.Columns> </DataGrid>Now this is my problem : I want to create a server, so I use many thread to run it (like my Login Listener). If I have a new connection (user) i want to write a log in my list, so i do this : Log log = new Log(Log.Flags.OK, "New co...");
But, I use multi threading, so my datagrid cannot update because it run on the primary thread...
I have this error (in French):
How can I resolve my problem ?
最满意答案
您需要允许主UI线程添加到集合中。
使用Dispatcher
public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; this.Dispatcher.Invoke((Action)(() => { logs.Add(this); } }You will need to allow the main UI thread to add to the collection.
Use Dispatcher for that
public Log(Flags flag, string message) { EtatLog = GetEnumDescription(flag); MessageLog = message; this.Dispatcher.Invoke((Action)(() => { logs.Add(this); } }更多推荐
发布评论