This is a migrated thread and some comments may be shown as answers.

[Solved] TreeView - LoadOnDemand Demo - Thread Safety?

3 Answers 144 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tim
Top achievements
Rank 1
Tim asked on 20 Jul 2009, 06:18 PM
I am referring to the Load-On-Demand demo here: http://demos.telerik.com/silverlight/#TreeView/LoadOnDemand

This is really cool use of the tree control.  I like it a lot, and I am using it.  However, in the real world, the

private RadTreeViewItem currentItem

causes thread safety issues if you try and expand more than one branch at a time (it only stores the last one clicked.)  In my case, I'm going after an async WCF service to get the information, so there is a measurable pause.

Is there a suggested way around this?  Somehow I will need to connect the WCF requests to the e.OriginalSource from the TreeView_LoadOn_Demand event.  I have two ideas in the works -

1. if there is already a value (!=null) in currentItem, then cancel the load-on-demand event
2. change the currentItem to a hash table of the original node and an identifier which I can associate with the WCF request, and match the two up when the WCF requests return (possibly out of the order in which I made them.)

Ideas welcomed.

Tim

3 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 21 Jul 2009, 08:09 AM
Hello Tim,

I think the recommended way to implement it by using option 1:
"1. if there is already a value (!=null) in currentItem, then cancel the load-on-demand event"

The users has a notification that the treeview is loading data so they will not be confused, and also it will keep your logic simple.

Please let us know if you need more help on that.

All the best,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Tim
Top achievements
Rank 1
answered on 21 Jul 2009, 12:39 PM
Option 1 doesn't "fix" the bug in the demo, nor is it as desirable as some other solution.  I.e., if my WCF service can handle 10 concurrent threads, then the user could certainly want to expand 10 concurrent nodes and it should work.

Does the following make sense?

        private RadTreeViewItem currentItem = null
        private void TreeView_LoadOnDemand(object sender, RadRoutedEventArgs e) 
        { 
            RadTreeViewItem item = e.OriginalSource as RadTreeViewItem; 
            if (currentItem == null) 
            { 
                currentItem = item
 
                System.Windows.Threading.DispatcherTimer pageTimer = new System.Windows.Threading.DispatcherTimer(); 
                pageTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); 
                pageTimer.Tick += loadOnDemandTimer_Tick; 
                pageTimer.Start(); 
            } 
            else 
            { 
                item.IsLoadingOnDemand = false
                e.Handled = true
            } 
        } 


Tim

0
Accepted
Bobi
Telerik team
answered on 24 Jul 2009, 06:40 AM
Hello Tim,

You can try some different approach, please take a look at the following article:

http://www.telerik.com/support/kb/silverlight/treeview/how-to-bind-radtreeview-to-hierarchical-data-and-use-load-on-demand.aspx

You can also try something like:
//some pseudo code

private void LoadNodes()
    {
.....
        foreach item in items
        {
            ThreadPool.QueueUserWorkItem(LoadItemDataCallback, item);
        }
.....
    }

private static void  LoadItemDataCallback(item)
{
...
    childItems = wcfClient.GetDataForItem(item);
    Action action = ()=> item.Items.AddRange(childItems);
    Dispatcher.Invoke(action);
}

I hope that this will help you. If you still have any other questions please send us some sample project or explain in more details the structure of your application.


Regards,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
TreeView
Asked by
Tim
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Tim
Top achievements
Rank 1
Bobi
Telerik team
Share this question
or