I
I
iRumba2015-04-02 08:56:58
.NET
iRumba, 2015-04-02 08:56:58

Why is another thread blocking the main form?

There is a procedure that fills the tree. Let's say

private void FillingTree()
{
for (int i=0; i<1000;i++)
{
TreeViewItem NewItem=new TreeViewItem();
NewItem.Header=i.ToString();
TreeView1.Dispatcher.Invoke( () => 
{
TreeView1.Items.Add(NewItem);
});
for (int j=0;j<1000;j++)
{
TreeViewItem NewSubItem=new TreeViewItem();
NewSubItem.Header=i.ToString() + "_" + j.ToString();
TreeView1.Dispatcher.Invoke( () => 
{
NewItem.Items.Add(NewSubItem);
});
}
}
}

and there is a procedure that creates a new thread and starts the procedure for filling the tree in it.
private void FillTreeAsync()
{
Thread th = new Thread(new ThreadStart(FillingTree));
th.Start();
}

Filling the tree takes a certain time and for this time both the tree itself and the form are blocked. Well, not that it would be directly blocked, but it freezes and twitches. Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2015-04-02
@Neuroware

TreeView1.Dispatcher.Invoke causes a block, during the execution of Items.Add, in order not to freeze the form, you can try to create a separate treeview in memory and fill it without Dispatcher.Invoke, and at the end, when the work is completed, transfer nodes to it using Dispatcher.Invoke.
But in this case, until the processing is completed, the treeview will be empty

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question