从后台工作人员加载listview [英] loading listview from a background worker
问题描述
我之前有这个代码,我以为我想出了一个解决方案,但是我发现一台机器上通常可以准备一个文件,并且其中有1000多个条目,并且将应用程序锁定得非常糟糕,以至于您在完成之前无法做任何事情.
我需要使用后台工作程序来读取文件,然后将项目列出到listview的帮助.我尝试在后台工作人员完成的情况下加载它.并且随着进步而改变.如果有人可以帮助我,那将是救生员.
i had this code up earlier and i thought i had figured out a resolution but i found a machine that had one file that i would normally ready and it had over 1000 entries in it and it locks the application up so bad that you cant do anything till it finishes.
I need help using a background worker to read the file and then list the items to the listview. I have tried loading it with background worker completed. and with progressed changed. if someone could help me with this it would be a life saver.
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
objsh = CreateObject("WScript.Shell")
strObject = "C:\Temp\ASERVICE.EDM"
Dim process = GetObject("winmgmts://./root/novadigm:NVD_Agent")
Dim method = process.Methods_("GetValue")
Dim inParameters = method.inParameters.SpawnInstance_()
inParameters.Path = strObject
Dim outParameters = process.ExecMethod_("NumberOfInstances", inParameters)
Dim StrHeaps = (outParameters.InstanceCount)
num_TextBox.Text = "Number of heaps in Aservice: " & StrHeaps
For i = 0 To StrHeaps Step +1
inParameters.Index = i
inParameters.Property = "ZOBJNAME"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim Value1 As String = outParameters.Value
inParameters.Property = "ZAVIS"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim Value2 As String = outParameters.Value
inParameters.Property = "NAME"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim Value3 As String = outParameters.Value
inParameters.Property = "INSTDATE"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim value5 As String = outParameters.Value
inParameters.Property = "ZSVCCSTA"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim Value7 As String = outParameters.Value
inParameters.Property = "ZVERIFY"
outParameters = process.ExecMethod_("GetValue", inParameters)
Dim Value8 As String = outParameters.Value
Dim str As String
Dim strArr() As String
Dim count As Integer
Dim value6 As String
str = value5
strArr = str.Split("T")
For count = 0 To strArr.Length - 2
value6 = (strArr(count))
Next
Dim value4 = ListView1.Items.Count
Dim item As New ListViewItem(value4)
item.SubItems.Add(Value1)
item.SubItems.Add(Value2)
item.SubItems.Add(Value7)
item.SubItems.Add(Value3)
If Value3 = "XXXX" Then
item.BackColor = Color.Gold
End If
item.SubItems.Add(value6)
item.SubItems.Add(Value8)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ListView1.Items.Add(item)
End Sub
我什至尝试将其加载到进度变化中,并且仍然可以正常工作.
I have even tried getting it to load on progress changed and it still dosent work.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ListView1.Items.Add(item)
End Sub
推荐答案
这是wpf的示例,但是可以对WinForms使用相同的概念.
THis is wpf example, but can use the same concepts for WinForms.
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
ItemsSource = new ObservableCollection<string>() { "pme", "two", 3.ToString() };
DataContext = this;
var bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.WorkerReportsProgress = true;
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerAsync();
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ItemsSource.Add(e.UserState.ToString());
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
var bw = (BackgroundWorker) sender;
for (int i = 1; i < 10000; i++)
bw.ReportProgress(i / 10000, i);
}
public ObservableCollection<string> ItemsSource { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
这篇关于从后台工作人员加载listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!