I have a method that reads from database and return result items one by one using yield
return. I call it in foreach
loop and at each iteration it invokes to STA thread
for update ProgressBar
. In this case I get about 6 seconds for all for some params. But if I remove Invoke
, then I get 28 seconds for the same params. I test this behavior in separate application and can say that with Invoke
each iterations to main thread processing in 10 times slower. However, Invoke
is allmost at 6 times faster (not slower!) in my application than in other completed examples.
Any suggestions?
In general it looks like this:
Thread thread = new Thread(() =>
{
int itemNumber = 0;
foreach (object item in SelectItems())
{
itemNumber++;
// doing some staff ...
Application.Current.Dispatcher.Invoke(
new Action(() =>
{
ProgressBar1.Value = itemNumber * 100 / count;
}),
null);
}
});
thread.IsBackground = true;
thread.Start();
Update
I think I need to say that is big application and there is about half hundred background threads. And I don't think they all can work at same time.