last day (16 days later) » 

5:50 PM
Good sir, may I consult you on your infinite wpf wisdom?
As the internet has thus far done me no good.
 
6:21 PM
@Ericafterdark asasdasdas
which means "I'm here", it's the fastest thing I can type out
LOL
how're you doing?
 
6:45 PM
Hey
Sorry, didn't think you were coming
I'm busy, but good. How are things with the fam?
 
awesome... my baby girl is growing up every day... she's 3 months now
 
That's beautiful man, congratulations.
 
=)
 
So! I am trying to make a heavy process a little more efficient by using multithreading. Think you could help me out?
 
are you using .Net 4.5?
or 4.0?
because things change radically with 4.5's async/await
 
6:54 PM
I think I'm in 4.0. How can I check for sure?
Yeah, 4
Just went to my app properties
 
=(
anyways
what's the issue?
 
Sorry man...
This is for my file input, or file open process. The routines that I run are pretty lengthy and with big files it will slow the program down to the point where the app freezes until it is done. I've learned that because of this I should open a thread in the background to do the work in, and just run a progress bar on the UI thread. Does this sound correct?
Or like the right approach?
 
yes
that's the correct approach
 
Cool. So my problem has to do with manipulating of the ObservableCollections that I use in my program
 
in 4.5 this gets drastically simplified because of async/await, but still, in 4.0 you can do it using Dispatcher and Tasks
 
7:02 PM
I've tried using something like this:
 
your heavy processes should return simple List<T>, not observablecolllections
 
    App.Current.Dispatcher.BeginInvoke((Action)delegate()
    {

    });
 
observablecollection is a UI-specific concept
your back-end processing code shouldn't have to deal with that
 
Okay, the funny thing is, I don't think it is. Let me show you what's exactly going on
For starters, here's a question that I posted on this topic: stackoverflow.com/questions/26431406/…
 
you're going back and forth between threads too much
that'll cause it to be even slower
my suggestion is
disable Collection notification until the actual work is done
then raise a CollectionChanged(Refresh) event
 
7:06 PM
By doing that does the control flow automatically push all of the notifications onto the UI thread when refreshed?
 
right.. you will need to create a class derived from observablecollection<T> that deals with this
and replace all your OC in all your VMs for the new one
but that's easy with ctrl+H
 
Shoot, I was hoping I wouldn't have to do that
 
something like this:
public class UICollection<T>: ObservableCollection<T>
    {
        public UICollection()
        {
            IsNotifying = true;
        }

        public bool IsNotifying { get; set; }

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (IsNotifying)
                base.OnPropertyChanged(e);
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (IsNotifying)
                base.OnCollectionChanged(e);
there's a lot from there that you don't really need
such as the SelectedItem
and the DisposableNotifier
you can do that with if(blah) if (!blah) and so forth
 
I also found this solution: tomlev2.wordpress.com/2009/04/17/…
So even if I push all of collection notifications at the end of the process I still need to create a custom ObservableCollection?
 
yep
because the default one won't allow you to "switch off" the notification when an item is inserted
you need to creeate your own and allow that
anyways doesn't seem like a very complicated thing
my version is more suitable for what you're doing
since you don't really need to notify the UI "step by step" but rather a whole "refresh" thing when the work is done
 
7:16 PM
Okay, sounds good. Let me get that class implemented.
Where does DisposableNotifier come from?
 
it doesn't
delete that
in fact
delete the entire DeferRefresh() concept
it's a tricky stuff I did because I don't like things like this:
myCollection.IsNotifying = false;
// work
myCollection.IsNotifying = true;
but you can do without it just fine
 
I got rid of DeferRefresh() and now the class I have is this:
    public class UICollection<T> : ObservableCollection<T>
    {
        public UICollection()
        {
            IsNotifying = true;
        }

        public bool IsNotifying { get; set; }

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (IsNotifying)
                base.OnPropertyChanged(e);
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (IsNotifying)
                base.OnCollectionChanged(e);
 
alright
delete the selected item thing
and change this:
 
I deleted the SelectedItem property
 
        public virtual void Load(IEnumerable<T> items)
        {
            if (items == null)
                throw new ArgumentNullException("items");

            this.IsNotifying = false;

            foreach (var item in items)
                this.Add(item);

            this.IsNotifying = true;
        }
 
7:26 PM
done
 
so, when you need to load a lot of items into the collection (from a background thread)
use Load()
hold on
public virtual void Load(IEnumerable<T> items)
{
if (items == null)
throw new ArgumentNullException("items");

this.IsNotifying = false;

foreach (var item in items)
this.Add(item);

this.IsNotifying = true;

this.Refresh();
}
you need the Refresh() method
put it back in place
 
So after this class is completed I need to change all of my ObservableCollections to this
 
yep
and then
in your loading code
use a background thread
and do collection.IsNotifying = false;
before you start adding items
and then
app.Current.Dispatcher.BeginInvoke((Action)(() => collection.Refresh()));
at the end of the entire thing
 
I have to do that for each collection the thread deals with?
 
yeah
 
7:30 PM
Okay, on changing all of my ObservableCollection to UICollection I seem to be getting errors due to built in methods and stuff that I could previously use with ObservableCollection
Like I can't use .Add()
Oh shoot, hold on
Okay, nevermind that. It seems that it doesn't like when I try to order the collection with .OrderBy
Like if I did this: BlockingTree.BlockTree = new UICollection<BlockingTreeModel>(BlockingTree.BlockTree.OrderBy(n => n.Rank));
It's because it doesn't contain a constructor that takes any arguments
Simpler version: collection = new UICollection<type>(collection.OrderBy(n => n.Value));
 
7:47 PM
add the ctor
 
Just like an empty overload?
 
public UICollection(Ienumerable<T> source)
{
this.Load(source)
}
;
sorry my bad
public UICollection(IEnumerable<>T source): this()
{
   this.Load(source);
}
that should do it
 
It did, and no problem man. I was trying to see if I could find the ms implementation of ObservableCollection to figure it out, lol
Okay so now I have to figure out all of the Collections being used by my background thread and turn their isNotifying prop to false, and then refresh them at the end of the thread? All in the confines of the background thread?
 
app.Current.Dispatcher.BeginInvoke((Action)(() => collection.Refresh()));


at the end of the entire thing
for all relevant collections you need refreshed
 
Okay cool. I think that all makes sense. There is a high possibility that some of the collections are dynamic and are created during the process, but I don't think that will be a problem
I'm dealing with a Collection bound TreeView, what if I just shut off that whole collection and then turned it back on instead of doing all the individuals?
 
8:08 PM
not sure it'll work
but you can try
 
I'm trying that right now. I got this exception in the UICollection class that you might want to take a look at. I think it's kind of similar to what happens with ObservableCollection when using other threads.
 
what's the stack trace? why is OnCollectionChanged() being called? it shouldn't
if you disabled IsNotifying it shouldn't call that
until you're back into the UI thread
 
Maybe it's because disabling the whole structure doesn't work
All of the collections that I would be working with right now are children of a larger collection. I was trying to turn isNotifying off just for that collection, and then refresh it at the end of the thread (both operations are still being done in the thread).
private void backgroundThread()
{
     collection.IsNotifying = false;

     //routines....

     app.Current.Dispatcher.BeginInvoke((Action)(() => collection.Refresh()));
}
 
nope, you'll have to one by one
 
Okay, i can do that. What about the ones that get created during the thread?
 
8:20 PM
shouldn't be a problem
anyways, a good practice is to have all your collection properties in any class be readonly and never return null
 
So don't worry about those? I'm using the `treeView` structure that we worked on earlier in the year.

Parent
-child
Parent
-child
-Grandchild
 
look
public class MyClass
{
     private List<int> myList;
     public List<int> MyList {get { return this.myList; } }

     public MyClass()
     {
         this.myList = new List<int>();
     }
}
see how the MyList property has no setter? you can't myclass.MyList = new ....
 
simply myclass.MyList.Add(5); will work
add items don't worry about instantiating the collection yourself
it's an implementation detail that outside classes don't care about
read about SRP
and encapsulation
 
I hear you. I only instantiate the class when I have to re-order it, like this:
node.Children.Add(CreateLocationNode(displayName, locationValue));
node.Children = new UICollection<HierarchicalVM>(node.Children.OrderBy(n => n.TreeView_LocValue));
 
8:24 PM
var ordered = node.Children.OrderBy(......);
node.Children.Clear();
node.Children.Load(ordered);
 
I was thinking, "why does that part matter if I just find all of the collections and set IsNotifying to false?", but I think that I'll have to do this for the dynamic collections that get added during the thread, correct?
 
yes
mutabilty and mutli threading are worst enemies of each other
all your classes should be immutable as much as possible, by design, until you really need to do otherwise
 
Of course they are, lol. I'll get on changing it.
There's something wrong with the code up there ^^^^ node.Children ends up with 0 children
 
8:41 PM
make sure the Load() method is actually adding the items
 
It skips the adding part
 
??
 
like there are no items to add
foreach (var item in items)
                this.Add(item);
 
call it like this:
 
public virtual void Load(IEnumerable<T> items)
        {
            if (items == null)
                throw new ArgumentNullException("items");

            this.IsNotifying = false;

            foreach (var item in items)
                this.Add(item); //SKIPS

            this.IsNotifying = true;

            this.Refresh();
        }
 
8:43 PM
node.Children.Load(ordered.ToList());
enumerate the collection before you traverse it
 
Sorry man, it still skips over that spot just the same
 
you're passing an empty collection
debug and shift+F9 over the items parameter
 
I'm passing it just like this: node.Children.Load(ordered.ToList());
Strangely, "items" receives a value of Count = 0 when in the Load loop
 
that's because ordered doesn't have anything
 
It doesn't have the ordered collection?
from this: var ordered = node.Children.OrderBy(n => n.TreeView_LocValue);
 
8:57 PM
not sure.. you'll have to debug further
 
Unfortunately I will have to save that for tomorrow. In the mean time, thanks again for all your help. I'll let you know if I hit any road blocks in what we've talked about.
 
=)
have a nice day =)
 

  last day (16 days later) »