日期:2014-05-18 浏览次数:21376 次
SynchronizationContext ctx = SynchronizationContext.Current; if (PropertyChanged != null) { if (ctx == null) { PropertyChanged(this, new PropertyChangedEventArgs(sender.GetType().ToString())); } else { ctx.Send(p => PropertyChanged(this, new PropertyChangedEventArgs(sender.GetType().ToString())), null); } }
------解决方案--------------------
public partial class MainWindow : Window { private int i = 0; private ObservableCollection<User> usrList = new ObservableCollection<User>(); public MainWindow() { InitializeComponent(); this.datagrid.ItemsSource = usrList; Thread thread = new Thread(new ThreadStart(Process)); thread.Start(); } void Process() { while (i++ < 10000) { Action action = () => { usrList.Add(new User(i.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fffffff"))); textshow.Text = string.Format("当前共产生{0}条数据", i.ToString()); }; this.Dispatcher.BeginInvoke(action); Thread.Sleep(10); } } }