Answer the question
In order to leave comments, you need to log in
How to properly populate an ObservableCollection in a loop and in a thread?
There is a DP class:
public class DP
{
public string Number { get; set; }
public string MachineName { get; set; }
public string UserName { get; set; }
public string IPAddress { get; set; }
}
ObservableCollection<DP> oCollectDP = new ObservableCollection<DP>();
foreach (var item in lstWorkStation) //Список List<string>lstWorkStation
{
DP dp = new DP();
string compName;
new Thread(()=>
{
compName = item;
dp.Number = oCollectDP.Count().ToString();
dp.MachineName = compName;
dp.UserName = getUserNameWMI(compName);
dp.IPAddress = getUserIP(compName);
Dispatcher.Invoke(new Action(() =>oCollectDP.Add(dp)));
}).Start();
}
Answer the question
In order to leave comments, you need to log in
look at the getUserNameWMi and getUserIP methods
and why do you need a stream?
Moreover, the process of creating a thread is resource-intensive and is not immediately created in the thread pool.
try async/await
but definitely remove thread creation threads
are good if they need to use mat solutions that freeze the GUI or the main thread.
1. remove thread creation
2. look at getUserNameWMI getUserIP methods
Решил сделать так:
Parallel.ForEach(lstWorkStation, currItem =>
{
DP dp = new DP();
dp.Number = oCollectDP.Count.ToString();
dp.MachineName = currItem;
dp.UserName = getUserNameWMI(currItem);
dp.IPAddress = getUserIP(currItem);
Dispatcher.Invoke(new Action(() =>
{
oCollectDP.Add(dp);
(sender as BackgroundWorker).ReportProgress(i);
}));
i++;
});
new Thread().Start() здесь лишний.
i++ нигде не юзается.
Dispatcher.Invoke(new Action(() =>
{
if (oCollectDP.Any(e => e.UserName != dp.UserName && e.IpAddress != dp.IpAddress )) // чтобы избежать дублирования
oCollectDP.Add(dp);
(sender as BackgroundWorker).ReportProgress(i);
}));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question