Background Process Event raising and Treeview

0
0

Hi,

See the attached Sample.

The form loads recursively the content of “C:\temp” into a TreeView control.

At Application start, all is good. TreeView is populated, and TreeNodes are working properly.

Pressing the button simulates a long running process using Task.Factory.StartNew(() =>{});

A onFinished Event is raised from inside this background Started Task.

This Event is handled in the Form code. When received it will reload the Treeview.

From Sub ‘BackgroundCompleted_Handler’ ,  The problems are :

  • The completion MessageBox isn’t shown.
  • The Treeview doesn’t accept any click. It will issue error “Cannot implicitly convert type ‘string’ to ‘Wisej.Web.TreeNode’.
  • Inside the Sub, VStudio is showing that we are still in the same thread (“Worker Thread”), but obviously not.
  • Trying to spawn the Treeview reload from the GUI thread using Application.StartTask(() => { Init_TreeView(); });
    doesn’t help.

If you expand some nodes before pressing the button, you see that the treeview is properly reloaded without generating an error.

If you don’t raise the event from inside the background task it’s behaving properly.

Can you help about what’s wrong with this sample ?

Regards.

  • You must to post comments
0
0

Hi Luca,

You wrongly understood the reason for the background process in my sample. It wasn’t intended to load the treeview, but to simulate a background long running process.

But no problem, your explanations and the modifications to the sample allowed me to solve partially the problem. It gives me a working solution, but I have to further investigate and test to develop the final optimal solution.

Your additional comments are more then welcome, even in the case it wouldn’t help me, which isn’t the case, it will help another guy from community.

I didn’t knew about the lazy mode.

Regarding the lazy load, I have the effect that a parent node without items underneath keeps the spinning when it get’s it’s triangle clicked.

I presume setting showloader=false for the clicked node solves this, but in the Buildtree I can’t find a reference to the clicked node. The selectednode is still the previous one.

Thanks for the lightning fast answer you gave to this previous post.

Regards.

  • Luca (ITG)
    Got it. About the “lazy” nodes. As long as the node has IsParent set to true it will always show as a parent node and will fire the expand events. When handling the Before or AfterExpand event you can decide if the node has not children and set it to a regular leaf node, set IsParent to false. This is useful for nodes that may not have children the first time they are expanded but you still want them to look like parent nodes for the next time they are expanded.
  • You must to post comments
1
0

The first problem I see is System.Threading.Tasks.Task.Factory.StartNew. This starts a new thread but the new thread knows absolutely nothing about the wisej context so any update to the client is lost. The thread fires immediately onFinished() which ends up in BackgroundCompleted_Handler. The first line is MessageBox.Show() which exists immediately with code DialogResult.Abort because it’s run out of context (or out of bound).

The next call is Init_TreeView() which works but it cannot update the client because it’s being run from an independent thread without having restored the session.

Use Application.StartTask() instead. It starts the task within the session context. Use Application.Update(this) to push the update to the client. When the client is affected by a background thread there is no request/response so the server has to push the updates to the client, or you have to hit refresh on the browser.

Two more comments 🙂

  • treeView1.SuspendLayout/ResumeLayout is not needed. Suspend/ResumeLayout suspends the layout of child controls when docked or anchored or in a layout panel. It lets you change a control size and location without triggering the layout engine.
  • There is a much better way to populate large trees: lazy loading. With Wisej it’s easy. Just populate the first level of nodes, set the property IsParent = true on the nodes (they will appear as parent nodes) and optionally set the property ShowLoader = true on the “lazy” parent nodes. Now when the user expands the node, Wisej will show a tiny loader in place of the open/close button while the server populates the children. On the server handle either BeforeExpand or AfterExpand and if the node has zero children, add only the first level children and so on. This way the nodes are populated as they are used with the neat loader spinning next to the node being expanded.

See attached modified sample.

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.