Home All Groups Group Topic Archive Search About

ObservableCollection(of T): Only for UI-tier or for all tiers?

Author
7 Apr 2007 6:39 AM
Henrik Dahl
Hello!

In some situations I have a collection of objects which it in different
situations could be relevant to deal with at the UI-tier and sometimes at
other tiers. Typically my concept is a composite so MyCollectionElementClass
has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI tier
using List(MyCollectionElementClass) or should I use the implementation
using ObservableCollection(MyCollectionElementClass) for all tiers?

A more general question: Should ObservableCollection(Of T) basically only be
used at UI-tiers due to some performance or infrastructural reasons for
instance?


Best regards,

Henrik Dahl

Author
7 Apr 2007 1:25 PM
Nicholas Paldino [.NET/C# MVP]
Henrik,

    I don't see why it shouldn't be used at all tiers.  ObservableCollection
doesn't have any UI specific functionality.  It is basically a Collection<T>
instance which implements INotifyCollectionChanged and
INotifyPropertyChanged.  These interfaces, while they can be used for UI,
don't HAVE to be strictly for UI.  You might want some code to run when
certain properties are changed (in a chain reaction sort of way).  The most
prominent example is UI changes, but not by any means the only example.

    Hope this helps.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
> Hello!
>
> In some situations I have a collection of objects which it in different
> situations could be relevant to deal with at the UI-tier and sometimes at
> other tiers. Typically my concept is a composite so
> MyCollectionElementClass has a collection of MyCollectionElementClass.
>
> Should I make one implementation for the UI-tier using
> ObservableCollection(MyCollectionElementClass) and one for the non-UI tier
> using List(MyCollectionElementClass) or should I use the implementation
> using ObservableCollection(MyCollectionElementClass) for all tiers?
>
> A more general question: Should ObservableCollection(Of T) basically only
> be used at UI-tiers due to some performance or infrastructural reasons for
> instance?
>
>
> Best regards,
>
> Henrik Dahl
>
>
Are all your drivers up to date? click for free checkup

Author
7 Apr 2007 2:46 PM
Henrik Dahl
Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception is
actually raised from the ObservableCollection(Of T) instance. This could
indicate there's something more into this ObservableCollection because what
you've described probably should not lead to such treading related
behaviour. What do you think?


Best regards,

Henrik Dahl

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
> Henrik,
>
>    I don't see why it shouldn't be used at all tiers.
> ObservableCollection doesn't have any UI specific functionality.  It is
> basically a Collection<T> instance which implements
> INotifyCollectionChanged and INotifyPropertyChanged.  These interfaces,
> while they can be used for UI, don't HAVE to be strictly for UI.  You
> might want some code to run when certain properties are changed (in a
> chain reaction sort of way).  The most prominent example is UI changes,
> but not by any means the only example.
>
>    Hope this helps.
>
>
> --
>          - Nicholas Paldino [.NET/C# MVP]
>          - mvp@spam.guard.caspershouse.com
>
> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>> Hello!
>>
>> In some situations I have a collection of objects which it in different
>> situations could be relevant to deal with at the UI-tier and sometimes at
>> other tiers. Typically my concept is a composite so
>> MyCollectionElementClass has a collection of MyCollectionElementClass.
>>
>> Should I make one implementation for the UI-tier using
>> ObservableCollection(MyCollectionElementClass) and one for the non-UI
>> tier using List(MyCollectionElementClass) or should I use the
>> implementation using ObservableCollection(MyCollectionElementClass) for
>> all tiers?
>>
>> A more general question: Should ObservableCollection(Of T) basically only
>> be used at UI-tiers due to some performance or infrastructural reasons
>> for instance?
>>
>>
>> Best regards,
>>
>> Henrik Dahl
>>
>>
>
>
Author
7 Apr 2007 3:22 PM
Nicholas Paldino [.NET/C# MVP]
Henrik,

    That isn't the case.  There is nothing in ObservableCollection which is
tied to the UI.  The error is expected, as you are trying to update the UI
thread from a non UI thread (something not allowed in WPF as well as Win32).


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
news:uzYP3NSeHHA.1220@TK2MSFTNGP03.phx.gbl...
> Hello Nicholas,
>
> Yes, but if you invoke some methods from a non-UI thread an exception is
> actually raised from the ObservableCollection(Of T) instance. This could
> indicate there's something more into this ObservableCollection because
> what you've described probably should not lead to such treading related
> behaviour. What do you think?
>
>
> Best regards,
>
> Henrik Dahl
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
> en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
>> Henrik,
>>
>>    I don't see why it shouldn't be used at all tiers.
>> ObservableCollection doesn't have any UI specific functionality.  It is
>> basically a Collection<T> instance which implements
>> INotifyCollectionChanged and INotifyPropertyChanged.  These interfaces,
>> while they can be used for UI, don't HAVE to be strictly for UI.  You
>> might want some code to run when certain properties are changed (in a
>> chain reaction sort of way).  The most prominent example is UI changes,
>> but not by any means the only example.
>>
>>    Hope this helps.
>>
>>
>> --
>>          - Nicholas Paldino [.NET/C# MVP]
>>          - mvp@spam.guard.caspershouse.com
>>
>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>>> Hello!
>>>
>>> In some situations I have a collection of objects which it in different
>>> situations could be relevant to deal with at the UI-tier and sometimes
>>> at other tiers. Typically my concept is a composite so
>>> MyCollectionElementClass has a collection of MyCollectionElementClass.
>>>
>>> Should I make one implementation for the UI-tier using
>>> ObservableCollection(MyCollectionElementClass) and one for the non-UI
>>> tier using List(MyCollectionElementClass) or should I use the
>>> implementation using ObservableCollection(MyCollectionElementClass) for
>>> all tiers?
>>>
>>> A more general question: Should ObservableCollection(Of T) basically
>>> only be used at UI-tiers due to some performance or infrastructural
>>> reasons for instance?
>>>
>>>
>>> Best regards,
>>>
>>> Henrik Dahl
>>>
>>>
>>
>>
>
>
Author
7 Apr 2007 5:12 PM
Henrik Dahl
Hello again!

OK, so your point is, that the exception is not thrown by
ObservableCollection(Of T) but by some action performed by an event handler
erroneously invoked by the non-UI thread in this case?


Best regards,

Henrik Dahl

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
en meddelelse news:eCPhAiSeHHA.596@TK2MSFTNGP06.phx.gbl...
> Henrik,
>
>    That isn't the case.  There is nothing in ObservableCollection which is
> tied to the UI.  The error is expected, as you are trying to update the UI
> thread from a non UI thread (something not allowed in WPF as well as
> Win32).
>
>
> --
>          - Nicholas Paldino [.NET/C# MVP]
>          - mvp@spam.guard.caspershouse.com
>
> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
> news:uzYP3NSeHHA.1220@TK2MSFTNGP03.phx.gbl...
>> Hello Nicholas,
>>
>> Yes, but if you invoke some methods from a non-UI thread an exception is
>> actually raised from the ObservableCollection(Of T) instance. This could
>> indicate there's something more into this ObservableCollection because
>> what you've described probably should not lead to such treading related
>> behaviour. What do you think?
>>
>>
>> Best regards,
>>
>> Henrik Dahl
>>
>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev
>> i en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
>>> Henrik,
>>>
>>>    I don't see why it shouldn't be used at all tiers.
>>> ObservableCollection doesn't have any UI specific functionality.  It is
>>> basically a Collection<T> instance which implements
>>> INotifyCollectionChanged and INotifyPropertyChanged.  These interfaces,
>>> while they can be used for UI, don't HAVE to be strictly for UI.  You
>>> might want some code to run when certain properties are changed (in a
>>> chain reaction sort of way).  The most prominent example is UI changes,
>>> but not by any means the only example.
>>>
>>>    Hope this helps.
>>>
>>>
>>> --
>>>          - Nicholas Paldino [.NET/C# MVP]
>>>          - mvp@spam.guard.caspershouse.com
>>>
>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>>>> Hello!
>>>>
>>>> In some situations I have a collection of objects which it in different
>>>> situations could be relevant to deal with at the UI-tier and sometimes
>>>> at other tiers. Typically my concept is a composite so
>>>> MyCollectionElementClass has a collection of MyCollectionElementClass.
>>>>
>>>> Should I make one implementation for the UI-tier using
>>>> ObservableCollection(MyCollectionElementClass) and one for the non-UI
>>>> tier using List(MyCollectionElementClass) or should I use the
>>>> implementation using ObservableCollection(MyCollectionElementClass) for
>>>> all tiers?
>>>>
>>>> A more general question: Should ObservableCollection(Of T) basically
>>>> only be used at UI-tiers due to some performance or infrastructural
>>>> reasons for instance?
>>>>
>>>>
>>>> Best regards,
>>>>
>>>> Henrik Dahl
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
7 Apr 2007 6:23 PM
Nicholas Paldino [.NET/C# MVP]
Henrik,

    Yes.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
news:eCJRafTeHHA.3536@TK2MSFTNGP04.phx.gbl...
> Hello again!
>
> OK, so your point is, that the exception is not thrown by
> ObservableCollection(Of T) but by some action performed by an event
> handler erroneously invoked by the non-UI thread in this case?
>
>
> Best regards,
>
> Henrik Dahl
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
> en meddelelse news:eCPhAiSeHHA.596@TK2MSFTNGP06.phx.gbl...
>> Henrik,
>>
>>    That isn't the case.  There is nothing in ObservableCollection which
>> is tied to the UI.  The error is expected, as you are trying to update
>> the UI thread from a non UI thread (something not allowed in WPF as well
>> as Win32).
>>
>>
>> --
>>          - Nicholas Paldino [.NET/C# MVP]
>>          - mvp@spam.guard.caspershouse.com
>>
>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>> news:uzYP3NSeHHA.1220@TK2MSFTNGP03.phx.gbl...
>>> Hello Nicholas,
>>>
>>> Yes, but if you invoke some methods from a non-UI thread an exception is
>>> actually raised from the ObservableCollection(Of T) instance. This could
>>> indicate there's something more into this ObservableCollection because
>>> what you've described probably should not lead to such treading related
>>> behaviour. What do you think?
>>>
>>>
>>> Best regards,
>>>
>>> Henrik Dahl
>>>
>>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev
>>> i en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
>>>> Henrik,
>>>>
>>>>    I don't see why it shouldn't be used at all tiers.
>>>> ObservableCollection doesn't have any UI specific functionality.  It is
>>>> basically a Collection<T> instance which implements
>>>> INotifyCollectionChanged and INotifyPropertyChanged.  These interfaces,
>>>> while they can be used for UI, don't HAVE to be strictly for UI.  You
>>>> might want some code to run when certain properties are changed (in a
>>>> chain reaction sort of way).  The most prominent example is UI changes,
>>>> but not by any means the only example.
>>>>
>>>>    Hope this helps.
>>>>
>>>>
>>>> --
>>>>          - Nicholas Paldino [.NET/C# MVP]
>>>>          - mvp@spam.guard.caspershouse.com
>>>>
>>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>>> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>>>>> Hello!
>>>>>
>>>>> In some situations I have a collection of objects which it in
>>>>> different situations could be relevant to deal with at the UI-tier and
>>>>> sometimes at other tiers. Typically my concept is a composite so
>>>>> MyCollectionElementClass has a collection of MyCollectionElementClass.
>>>>>
>>>>> Should I make one implementation for the UI-tier using
>>>>> ObservableCollection(MyCollectionElementClass) and one for the non-UI
>>>>> tier using List(MyCollectionElementClass) or should I use the
>>>>> implementation using ObservableCollection(MyCollectionElementClass)
>>>>> for all tiers?
>>>>>
>>>>> A more general question: Should ObservableCollection(Of T) basically
>>>>> only be used at UI-tiers due to some performance or infrastructural
>>>>> reasons for instance?
>>>>>
>>>>>
>>>>> Best regards,
>>>>>
>>>>> Henrik Dahl
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
7 Apr 2007 7:34 PM
Henrik Dahl
OK.

Do you know if instances of ObservableCollection(Of T) may also be
marshalled via the serialization mechanishms of WCF or via XmlSerializer?


Best regards,

Henrik Dahl

Show quoteHide quote
"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
en meddelelse news:%23LIuXHUeHHA.4308@TK2MSFTNGP02.phx.gbl...
> Henrik,
>
>    Yes.
>
>
> --
>          - Nicholas Paldino [.NET/C# MVP]
>          - mvp@spam.guard.caspershouse.com
>
> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
> news:eCJRafTeHHA.3536@TK2MSFTNGP04.phx.gbl...
>> Hello again!
>>
>> OK, so your point is, that the exception is not thrown by
>> ObservableCollection(Of T) but by some action performed by an event
>> handler erroneously invoked by the non-UI thread in this case?
>>
>>
>> Best regards,
>>
>> Henrik Dahl
>>
>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev
>> i en meddelelse news:eCPhAiSeHHA.596@TK2MSFTNGP06.phx.gbl...
>>> Henrik,
>>>
>>>    That isn't the case.  There is nothing in ObservableCollection which
>>> is tied to the UI.  The error is expected, as you are trying to update
>>> the UI thread from a non UI thread (something not allowed in WPF as well
>>> as Win32).
>>>
>>>
>>> --
>>>          - Nicholas Paldino [.NET/C# MVP]
>>>          - mvp@spam.guard.caspershouse.com
>>>
>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>> news:uzYP3NSeHHA.1220@TK2MSFTNGP03.phx.gbl...
>>>> Hello Nicholas,
>>>>
>>>> Yes, but if you invoke some methods from a non-UI thread an exception
>>>> is actually raised from the ObservableCollection(Of T) instance. This
>>>> could indicate there's something more into this ObservableCollection
>>>> because what you've described probably should not lead to such treading
>>>> related behaviour. What do you think?
>>>>
>>>>
>>>> Best regards,
>>>>
>>>> Henrik Dahl
>>>>
>>>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com>
>>>> skrev i en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
>>>>> Henrik,
>>>>>
>>>>>    I don't see why it shouldn't be used at all tiers.
>>>>> ObservableCollection doesn't have any UI specific functionality.  It
>>>>> is basically a Collection<T> instance which implements
>>>>> INotifyCollectionChanged and INotifyPropertyChanged.  These
>>>>> interfaces, while they can be used for UI, don't HAVE to be strictly
>>>>> for UI.  You might want some code to run when certain properties are
>>>>> changed (in a chain reaction sort of way).  The most prominent example
>>>>> is UI changes, but not by any means the only example.
>>>>>
>>>>>    Hope this helps.
>>>>>
>>>>>
>>>>> --
>>>>>          - Nicholas Paldino [.NET/C# MVP]
>>>>>          - mvp@spam.guard.caspershouse.com
>>>>>
>>>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>>>> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>>>>>> Hello!
>>>>>>
>>>>>> In some situations I have a collection of objects which it in
>>>>>> different situations could be relevant to deal with at the UI-tier
>>>>>> and sometimes at other tiers. Typically my concept is a composite so
>>>>>> MyCollectionElementClass has a collection of
>>>>>> MyCollectionElementClass.
>>>>>>
>>>>>> Should I make one implementation for the UI-tier using
>>>>>> ObservableCollection(MyCollectionElementClass) and one for the non-UI
>>>>>> tier using List(MyCollectionElementClass) or should I use the
>>>>>> implementation using ObservableCollection(MyCollectionElementClass)
>>>>>> for all tiers?
>>>>>>
>>>>>> A more general question: Should ObservableCollection(Of T) basically
>>>>>> only be used at UI-tiers due to some performance or infrastructural
>>>>>> reasons for instance?
>>>>>>
>>>>>>
>>>>>> Best regards,
>>>>>>
>>>>>> Henrik Dahl
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
8 Apr 2007 5:40 AM
Nicholas Paldino [.NET/C# MVP]
Henrik,

    Well, did you check the documentation?  *smiles*

    It says that the Serializable attribute is applied to it, which means
that instances of it can be serialized, assuming that T is serializable.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
news:OI5NGvUeHHA.3648@TK2MSFTNGP05.phx.gbl...
> OK.
>
> Do you know if instances of ObservableCollection(Of T) may also be
> marshalled via the serialization mechanishms of WCF or via XmlSerializer?
>
>
> Best regards,
>
> Henrik Dahl
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev i
> en meddelelse news:%23LIuXHUeHHA.4308@TK2MSFTNGP02.phx.gbl...
>> Henrik,
>>
>>    Yes.
>>
>>
>> --
>>          - Nicholas Paldino [.NET/C# MVP]
>>          - mvp@spam.guard.caspershouse.com
>>
>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>> news:eCJRafTeHHA.3536@TK2MSFTNGP04.phx.gbl...
>>> Hello again!
>>>
>>> OK, so your point is, that the exception is not thrown by
>>> ObservableCollection(Of T) but by some action performed by an event
>>> handler erroneously invoked by the non-UI thread in this case?
>>>
>>>
>>> Best regards,
>>>
>>> Henrik Dahl
>>>
>>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> skrev
>>> i en meddelelse news:eCPhAiSeHHA.596@TK2MSFTNGP06.phx.gbl...
>>>> Henrik,
>>>>
>>>>    That isn't the case.  There is nothing in ObservableCollection which
>>>> is tied to the UI.  The error is expected, as you are trying to update
>>>> the UI thread from a non UI thread (something not allowed in WPF as
>>>> well as Win32).
>>>>
>>>>
>>>> --
>>>>          - Nicholas Paldino [.NET/C# MVP]
>>>>          - mvp@spam.guard.caspershouse.com
>>>>
>>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>>> news:uzYP3NSeHHA.1220@TK2MSFTNGP03.phx.gbl...
>>>>> Hello Nicholas,
>>>>>
>>>>> Yes, but if you invoke some methods from a non-UI thread an exception
>>>>> is actually raised from the ObservableCollection(Of T) instance. This
>>>>> could indicate there's something more into this ObservableCollection
>>>>> because what you've described probably should not lead to such
>>>>> treading related behaviour. What do you think?
>>>>>
>>>>>
>>>>> Best regards,
>>>>>
>>>>> Henrik Dahl
>>>>>
>>>>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com>
>>>>> skrev i en meddelelse news:e7$oGhReHHA.588@TK2MSFTNGP06.phx.gbl...
>>>>>> Henrik,
>>>>>>
>>>>>>    I don't see why it shouldn't be used at all tiers.
>>>>>> ObservableCollection doesn't have any UI specific functionality.  It
>>>>>> is basically a Collection<T> instance which implements
>>>>>> INotifyCollectionChanged and INotifyPropertyChanged.  These
>>>>>> interfaces, while they can be used for UI, don't HAVE to be strictly
>>>>>> for UI.  You might want some code to run when certain properties are
>>>>>> changed (in a chain reaction sort of way).  The most prominent
>>>>>> example is UI changes, but not by any means the only example.
>>>>>>
>>>>>>    Hope this helps.
>>>>>>
>>>>>>
>>>>>> --
>>>>>>          - Nicholas Paldino [.NET/C# MVP]
>>>>>>          - mvp@spam.guard.caspershouse.com
>>>>>>
>>>>>> "Henrik Dahl" <HenrikDahl@community.nospam> wrote in message
>>>>>> news:uEdZ49NeHHA.4868@TK2MSFTNGP06.phx.gbl...
>>>>>>> Hello!
>>>>>>>
>>>>>>> In some situations I have a collection of objects which it in
>>>>>>> different situations could be relevant to deal with at the UI-tier
>>>>>>> and sometimes at other tiers. Typically my concept is a composite so
>>>>>>> MyCollectionElementClass has a collection of
>>>>>>> MyCollectionElementClass.
>>>>>>>
>>>>>>> Should I make one implementation for the UI-tier using
>>>>>>> ObservableCollection(MyCollectionElementClass) and one for the
>>>>>>> non-UI tier using List(MyCollectionElementClass) or should I use the
>>>>>>> implementation using ObservableCollection(MyCollectionElementClass)
>>>>>>> for all tiers?
>>>>>>>
>>>>>>> A more general question: Should ObservableCollection(Of T) basically
>>>>>>> only be used at UI-tiers due to some performance or infrastructural
>>>>>>> reasons for instance?
>>>>>>>
>>>>>>>
>>>>>>> Best regards,
>>>>>>>
>>>>>>> Henrik Dahl
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
9 Apr 2007 12:17 PM
Linda Liu [MSFT]
Hi Henrik,

I agree that ObservableCollection(Of T) can be used for all tiers.

>  the exception is not thrown by ObservableCollection(Of T) but by some
action performed by an event handler erroneously invoked by the non-UI
thread in this case?

In my opinion, because the ObservableCollection(Of T) implements the
INotifyCollectionChanged and INotifyPropertyChanged interfaces, it will
cause UI update when the ObservableCollection(Of T) is changed.

Let's say a non-UI thread accesses and changes the ObservableCollection(Of
T), then it is the non-UI thread that performs the UI update in the next
step, which is not allowed in the thread models of both WinForms and WPF
applications.

In WinForms world, the solution is to define a delegate for the method to
access the control, and then call the Invoke or BeginInvoke method of the
WinForms control to invoke the delegate.

In WPF world, the solution is similar but is a little different. We still
need to define a delegate for the method to access the UI element, and then
call the Invoke or BeginInvoke method of the Dispather linked to the UI
thread.

The following is a sample.

delegate void SetTextBoxDelegate();
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new
SetTextBoxDelegate(SetTextBox1));
void SetTextBox1()
  {           
            this.textBox1.Text = "some text";
   }

For more information on the threading model in WPF, you may visit the
following link:

'Threading Model'
http://msdn2.microsoft.com/en-us/library/ms741870.aspx

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Bookmark and Share