Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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.

VB
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    ListView1.Items.Add(item)

End Sub
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-12 20:33pm    
So, VB or VB.NET? Yes, this is VB.NET. Please remove the tag "VB".
--SA

1 solution

THis is wpf example, but can use the same concepts for WinForms.

XML
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;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900