PC Review


Reply
Thread Tools Rate Thread

DataBinding to a singleton collection

 
 
Torben Frandsen
Guest
Posts: n/a
 
      3rd Aug 2004
Hi

I have a collection which is updated from two different threads, using the
singleton pattern. The GetInstance method is of course thread safe. Works
nicely.

Now I want the UI to reflect what's going on in the collection, and what's
more obvious than implementing IBindingList and binding a DataGrid to it?

This may not be a good idea after all, since the DataGrid updates only every
now and then, and the form hangs on closing. I suppose this is a thread
safety issue.

Is there something I can do with my collection to make it more databinding
friendly, or should I invoke a method on the UI thread every time a field is
changed? Or is there a third way?

Torben


 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      3rd Aug 2004
Hi,

A collection is by default not thread safe, if you are enumerating a
collection you cannot add an element to it, it would rise an exception.

Take a look at skeet's singleton implementation to know how to do a thread
safe singleton class.

Of course this has nothing to do with making the collection operations
threadsafe. You have to do this by your own using some locking mechanism.

I would not implement the IBindingList , what I would do in this case is
implement an event in your collection that is risen when a new element is
added to the collection, doing so will allow you to do whatever you want on
the interface.

The form hangs probably cause the worker thread is not done yet.

Hope this help


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



"Torben Frandsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi
>
> I have a collection which is updated from two different threads, using the
> singleton pattern. The GetInstance method is of course thread safe. Works
> nicely.
>
> Now I want the UI to reflect what's going on in the collection, and what's
> more obvious than implementing IBindingList and binding a DataGrid to it?
>
> This may not be a good idea after all, since the DataGrid updates only

every
> now and then, and the form hangs on closing. I suppose this is a thread
> safety issue.
>
> Is there something I can do with my collection to make it more databinding
> friendly, or should I invoke a method on the UI thread every time a field

is
> changed? Or is there a third way?
>
> Torben
>
>



 
Reply With Quote
 
Torben Frandsen
Guest
Posts: n/a
 
      4th Aug 2004
Ignacio Machin ( .NET/ C# MVP ) wrote:

> A collection is by default not thread safe


I figured as much :-)

> Of course this has nothing to do with making the collection operations
> threadsafe. You have to do this by your own using some locking
> mechanism.


So what you mean is, it's not sufficient to take out a lock whenever an
instance is requested? When else should I take out a lock? When I request an
item? When I change an item? Everywhere?

> I would not implement the IBindingList , what I would do in this
> case is implement an event in your collection that is risen when a
> new element is added to the collection, doing so will allow you to do
> whatever you want on the interface.


And in the end I might have to do that. I just wouldn't have thought my
approach was so unusual.

Torben


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with combobox databinding and Items (collection) Rick Shaw Microsoft C# .NET 1 10th Aug 2006 06:22 AM
DataBinding to Properties of a Collection charlie@noemail.com Microsoft C# .NET 2 10th Mar 2005 03:34 AM
Singleton and garbage collection Stephen Brown Microsoft C# .NET 7 22nd Jun 2004 01:02 AM
Is Singleton collection of Singletons possible?? Daniel Billingsley Microsoft C# .NET 17 15th Mar 2004 08:49 AM
Databinding and a Typed Collection solex Microsoft VB .NET 1 4th Feb 2004 06:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:07 PM.