using System.ComponentModel; using System.Threading; namespace DelftTools.Utils.Threading { /// /// Provides a thread-safe binding-list by using the synchronizationcontext where the list was created. /// HACK: Probably not full-proof. /// Copied from: http://groups.google.co.uk/group/microsoft.public.dotnet.languages.csharp/msg/f12a3c5980567f06 /// List should be created in a forms control providing a synchronization context /// More about synchronizationcontext here: http://www.codeproject.com/KB/cpp/SyncContextTutorial.aspx /// /// TODO: migrate to use InvokeRequired aspect /// /// public class ThreadsafeBindingList : BindingList { private readonly SynchronizationContext synchronizationContext; /// /// Creates a bindinglist using the givin synchronization context /// public ThreadsafeBindingList(SynchronizationContext context) { if (context == null) { context = new SynchronizationContext(); } synchronizationContext = context; } protected override void OnAddingNew(AddingNewEventArgs e) { synchronizationContext.Send(delegate { BaseAddingNew(e); }, null); } protected override void OnListChanged(ListChangedEventArgs e) { synchronizationContext.Send( delegate { BaseListChanged(e); }, null); } private void BaseAddingNew(AddingNewEventArgs e) { base.OnAddingNew(e); } private void BaseListChanged(ListChangedEventArgs e) { base.OnListChanged(e); } } }