Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

DataBinding to a singleton collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Torben Frandsen - 03 Aug 2004 14:18 GMT
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
Ignacio Machin \( .NET/ C#  MVP \) - 03 Aug 2004 14:42 GMT
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

Signature

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

> Hi
>
[quoted text clipped - 14 lines]
>
> Torben
Torben Frandsen - 04 Aug 2004 12:58 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.