Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

How can i report muiltple reportprogress values from backgroundworker dowork event to a dataGridView ?

$
0
0

First i added this class:

public class MyProgress
        {
            public string Id { get; set; }
            public string Progress { get; set; }
        }


Then the dowork event:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                List<IntPtr> intptrs = GetProcessesIntptrList();
                for (int x = 0; x < intptrs.Count ; x++)
                {
                    GetProcessInfo(intptrs[x]);
                }
                    while (true)
                    {
                        List<MyProgress> prog = new List<MyProgress>();

                        prog = new List<MyProgress>();

                        procList = Process.GetProcesses().ToList();
                        for (int i = 0; i < procList.Count; i++)
                        {
                            Process[] processes = Process.GetProcessesByName(procList[i].ProcessName);
                            PerformanceCounter performanceCounter = new PerformanceCounter();
                            performanceCounter.CategoryName = "Process";
                            performanceCounter.CounterName = "Working Set - Private";//"Working Set";
                            performanceCounter.InstanceName = processes[0].ProcessName;

                            prog.Add(new MyProgress { Id = procList[i].ProcessName, Progress = ((uint)performanceCounter.NextValue() / 1024).ToString("N0") });

                            worker.ReportProgress(0, prog);
                        }
                    }
            }
        }


And last the backgroundworker progresschanged event where i want to add the values of each process to the datagridview1 rows under cell 3.

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            foreach (MyProgress p in (e.UserState as List<MyProgress>))
            {
                currentRow.Cells[3].Value = p.Progress;
                dataGridView1.Rows.Add(
                    p.Progress);
            }
        }

The variable currentRow not exit yet. And i know that cell 3 is where i want to add all the rows of the processes values.

And i also don't know how many rows there should be.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>