Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4611662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:53+00:00 2026-05-22T01:17:53+00:00

This is a two part question. I have a class that gets all processes

  • 0

This is a two part question.
I have a class that gets all processes asynchronously and polls them for CPU usage. Yesterday I had a bug with it and it was solved here.
The first part of the question is why the solution helped. I didn’t understand the explanation.
The second part of the question is that I still get an "Object reference not set to an instance of object" exception occasionally when I try to print the result at the end of the process. This is because item.Key is indeed null. I don’t understand why that is because I put a breakpoint checking for (process == null) and it was never hit. What am I doing wrong?
Code is below.

class ProcessCpuUsageGetter
    {
        private IDictionary<Process, int> _usage;

        public IDictionary<Process, int> Usage { get { return _usage; } }

        public ProcessCpuUsageGetter()
        {
            while (true)
            {
                Process[] processes = Process.GetProcesses();
                int processCount = processes.Count();
                Task[] tasks = new Task[processCount];

                _usage = new Dictionary<Process, int>();
                for (int i = 0; i < processCount; i++)
                {
                    var localI = i;
                    var localProcess = processes[localI];
                    tasks[localI] = Task.Factory.StartNew(() => DoWork(localProcess));
                }
                Task.WaitAll(tasks);

                foreach (var item in Usage)
                {
                    Console.WriteLine("{0} - {1}%", item.Key.ProcessName, item.Value);
                }                
            }
        }

        private void DoWork(object o)
        {
            Process process = (Process)o;
            PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
            pc.NextValue();
            Thread.Sleep(1000);
            int cpuPercent = (int)pc.NextValue() / Environment.ProcessorCount;
            if (process == null)
            {
                var x = 5;
            }
            if (_usage == null)
            {
                var t = 6;
            }
            _usage.Add(process, cpuPercent);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:17:53+00:00Added an answer on May 22, 2026 at 1:17 am

    The line

     _usage.Add(process, cpuPercent);
    

    is accessing a not-threadsafe collection from a thread.

    Use a ConcurrentDictionary<K,V> instead of the normal dictionary.

    The ‘null reference’ error is just a random symptom, you could get other errors too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two part question. A dumb technical query and a broader query
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is a two part question. The first is a specific question about DataSets
This is somewhat a two-part question (please let me know if they should be
I have a two part question. Question 1. I've always thought a folder was
The scenario is this We have two applications A and B, both which are
This question has been discussed in two blog posts ( http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/ , http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/ ),
This is on the Mac: If I have two filenames /foo/foo and /foo/FOO they
This is tricky to explain. We have a DataTable that contains a user configurable
I've run into this problem with testing. Let's assume I have two models, User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.